2016-01-26 103 views
-4
public class MainActivity extends Activity{ 

Spinner spinner; 
String values[]; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
      .detectAll() 
      .penaltyLog() 
      .penaltyDialog() 
      .build()); 

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll() 
      .penaltyLog() 
      .build()); 

    TextView wid = (TextView) findViewById(R.id.wid); 
    TextView name = (TextView) findViewById(R.id.name); 
    TextView url = (TextView) findViewById(R.id.url); 

    JSONObject json = null; 
    String str = ""; 
    HttpResponse response; 
    HttpClient myClient = new DefaultHttpClient(); 
    HttpPost myConnection = new HttpPost("http://egcrm.cloudapp.net/operations/index.php/dcapi/gettrucktypes"); 
    //http://demos.tricksofit.com/files/json.php 

    try { 
     response = myClient.execute(myConnection); 
     str = EntityUtils.toString(response.getEntity(), "UTF-8"); 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     JSONArray jArray = new JSONArray(str); 
     for (int i=0; i < jArray.length();i++){ 
     json = jArray.getJSONObject(i); 

     wid.setText(json.getString("Key")); 
     name.setText(json.getString("Value")); 
     // url.setText(json.getString("url")); 
      String shiva=new String(); 
      shiva=json.getString("Value"); 
      values=new String[jArray.length()]; 
      values[i]=shiva; 
    } 

    } 

    catch (JSONException e) { 
     e.printStackTrace(); 
    } 
spinner=(Spinner)findViewById(R.id.spinner); 
     ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,values); 
     aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(aa); 
    } 

} 

的logcat:iam得到一個空指針異常。幫我解決它

11月1日至26日:56:12.799 19322-19322/com.example.sai.lasttry E/AndroidRuntime:致命異常:主要 過程:com。示例。 sai.lasttry,PID:19322 java.lang.NullPointerException:嘗試在空對象引用 處調用虛方法'java.lang.String java.lang.Object.toString()'at android.widget.ArrayAdapter.createViewFromResource( ArrayAdapter.java:394) at android.widget.AbsSpinner.onM android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) at android.widget.AbsSpinner.onM在android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins()方法android.widget.Spinner.onMeasure(Spinner.java:507) ViewGroup.java:5463) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 在android.widget.LinearLayout.measureHorizo​​ntal(LinearLayout.java:1083) 在android.widget.LinearLayout.onMeasure(LinearLayout中。 java:615) at android.view.View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at andro android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 在android上的android.view.View.measure(View.java:17430) 的id.widget.FrameLayout.onMeasure(FrameLayout.java:430) 。 widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view。 View.measure(View.java:17430) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2560) at android.view.View.measure(View.java:17430) at android.view.ViewRootImpl.performMeasure( ViewRootImpl.java:2001) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl。 java:1054) at android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779) at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) at android .view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) at android .os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app .ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method。Java的:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

+1

,請複製粘貼控制檯輸出 –

+1

添加您的logcat – Pavya

+1

編輯您的問題 – Japongskie

回答

0

根源:新的String []中環

values=new String[jArray.length()]; 
0

,我認爲你的錯誤是在這一行

str = EntityUtils.toString(response.getEntity(), "UTF-8"); 

似乎你試圖讓數據未發佈,所以做這樣

HttpGet httpGet = new HttpGet("http://egcrm.cloudapp.net/operations/index.php/dcapi/gettrucktypes"); 
HttpResponse httpResponse = new DefaultHttpClient() 
        .execute(httpGet); 

然後獲取後的字符串像

String result = EntityUtils.toString(httpResponse.getEntity(), 
       "utf-8"); 
0

錯誤是因爲論文線:

values=new String[jArray.length()]; 
values[i]=shiva; 

,這是因爲值的數組沒有填滿,和它給NullPointerException。 嘗試調試並檢查jArray.length()中的內容,並確保它填滿了您的值數組。