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.measureHorizontal(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)
,請複製粘貼控制檯輸出 –
添加您的logcat – Pavya
編輯您的問題 – Japongskie