2016-04-29 167 views
0

我做我的課項目的最後,一切都很順利,然後我嘗試運行應用程序,崩潰,我的手機上,這:致命異常:主(學校項目)

04- 29 13:36:24.185 3507-3507/com.example.jessewmoore.database2 E/AndroidRuntime:致命異常:主要 進程:com.example.jessewmoore.database2,PID:3507 java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.jessewmoore.database2/com.example.jessewmoore.database2.MainActivity}:java.lang.NullPointerException:嘗試在空對象引用 上調用虛方法'int java.lang.String.length()' at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294) at android.app.ActivityThread.access $ 1000(ActivityThread.java:210) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1704) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372 ) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 引起來自:java.lang .NullPointerException:嘗試在org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) 處的空對象引用 上調用虛方法'int java.lang.String.length()'at org.json.JSONTokener.nextValue (JSONTokener.java:94) at org.json.JSONArray。(JSONArray.java:92) at org.json.JSONArray。(JSONArray.java:108) at com.example.jessewmoore.database2.MainActivity.onCreate (MainActivity.java:72) 在android.app.Activity.performCreate(Activity.java:6575) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3137) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294) at android.app.ActivityThread.access $ 1000(ActivityThread.java:210) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1704) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) 在android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

每一件事情建議立即進行刪除DBE好,沒有錯誤。

activity_main

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity"> 

<HorizontalScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:focusableInTouchMode="true" 
    android:focusable="true"> 

    <TableLayout android:id="@+id/table" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:focusableInTouchMode="true" 
     android:focusable="true"></TableLayout> 

</HorizontalScrollView> 
</ScrollView> 

AndroidManifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.jessewmoore.database2"> 
<uses-permission android:name="android.permission.INTERNET"/> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

MainActivity

package com.example.jessewmoore.database2; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.TableRow.LayoutParams; 

public class MainActivity extends Activity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    String result = null; 
    InputStream is = null; 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http: busdata.pe.hu/myfile.php"); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 

     Log.e("log_tag", "connection_success"); 
     // Toast.makeText(getApplicationContext(), 「pass」,       Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) { 
     Log.e("log_tag", "Error in http connection" + e.toString()); 
     Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show(); 

    } 
    //convert response to string 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
      // Toast.makeText(getApplicationContext(), 「Input Reading pass」, Toast.LENGTH_SHORT).show(); 
     } 
     is.close(); 

     result = sb.toString(); 
    } catch (Exception e) { 
     Log.e("log_tag", "Error converting results" + e.toString()); 
     Toast.makeText(getApplicationContext(), "Input reading fail", Toast.LENGTH_SHORT).show(); 

    } 

    //parse json data 
    try { 
     JSONArray jArray = new JSONArray(result); 
     TableLayout tv = (TableLayout) findViewById(R.id.table); 
     tv.removeAllViewsInLayout(); 
     int flag = 1; 
     for (int i = -1; i < jArray.length() - 1; i++) { 
      TableRow tr = new TableRow(MainActivity.this); 
      tr.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
      if (flag == 1) { 
       TextView b6 = new TextView(MainActivity.this); 
       b6.setText("Id"); 
       b6.setTextColor(Color.BLUE); 
       b6.setTextSize(15); 
       tr.addView(b6); 
       TextView b19 = new TextView(MainActivity.this); 
       b19.setPadding(10, 0, 0, 0); 
       b19.setTextSize(15); 
       b19.setText("Name"); 
       b19.setTextColor(Color.BLUE); 
       tr.addView(b19); 
       TextView b29 = new TextView(MainActivity.this); 
       b29.setPadding(10, 0, 0, 0); 
       b29.setText("Status"); 
       b29.setTextColor(Color.BLUE); 
       b29.setTextSize(15); 
       tr.addView(b29); 
       tv.addView(tr); 
       final View vline = new View(MainActivity.this); 
       vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2)); 
       vline.setBackgroundColor(Color.BLUE); 
       tv.addView(vline); 
       flag = 0; 
      } else { 
       JSONObject json_data = jArray.getJSONObject(i); 
       Log.i("log_tag", "id: " + json_data.getInt("Id") + ", Username: " + json_data.getString("username") + ", No:" + json_data.getString("comment")); 
       TextView b = new TextView(MainActivity.this); 
       String stime = String.valueOf(json_data.getInt("Id")); 
       b.setText(stime); 
       b.setTextColor(Color.RED); 
       b.setTextSize(15); 
       tr.addView(b); 
       TextView b1 = new TextView(MainActivity.this); 
       b1.setPadding(10, 0, 0, 0); 
       b1.setTextSize(15); 
       String stime1 = json_data.getString("username"); 
       b1.setText(stime1); 
       b1.setTextColor(Color.BLACK); 
       tr.addView(b1); 
       TextView b2 = new TextView(MainActivity.this); 
       b2.setPadding(10, 0, 0, 0); 
       String stime2 = json_data.getString("comment"); 
       b2.setText(stime2); 
       b2.setTextColor(Color.BLACK); 
       b2.setTextSize(15); 
       tr.addView(b2); 
       tv.addView(tr); 
       final View vline1 = new View(MainActivity.this); 
       vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
       vline1.setBackgroundColor(Color.WHITE); 
       tv.addView(vline1); 
      } 
     } 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data" + e.toString()); 
     Toast.makeText(getApplicationContext(), "JsonArray_fail", Toast.LENGTH_SHORT).show(); 
    } 

    } 

    } 
+0

是否有任何日誌消息? –

+0

從'result'創建'JSONArray'時發生錯誤。結果格式是否正確? – AgileNinja

+0

@Saviour自己不是我所知的 –

回答

0

你的錯誤我SA的NullPointerException,在MainActivity.java引起線72它是:

JSONArray jArray = new JSONArray(result); 

爲了進一步讀入你的錯誤,似乎JSONArray試圖調用上結果長度(),可能同時將其試圖構建適當的JSON數組來自字符串。能夠理解這些錯誤對於解決您的問題非常重要,並且可以幫助您提出更好的問題!

您最初將結果設置爲空 - 它看起來在調用新的JSONArray(result)之前沒有正確設置。開始的一個好地方是檢查直接在它初始化結果之前的try塊是否正確完成並且不會失敗。也許你在'轉換字符串響應'try塊的catch語句錯誤之前在日誌中有東西。

+0

結果在數組之前設置爲這個右邊String result = null; –

+0

雖然在try catch塊內。 try塊可能會遇到異常,然後結果永遠不會被設置。相反,你會輸出「錯誤轉換結果」到你的日誌。嘗試使用AgileNinja建議的斷點進行調試,並且您應該在第72行找到結果爲null。*編輯,因爲您已將您的評論更改爲顯示result = null:這正是我的觀點... – HunterCM

+0

請參閱最近編輯 –