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的:
01-26 11:56:12.799 19322-19322 / com.example.sai.lasttry E / AndroidRuntime:致命异常:主要 处理:com.example.sai.lasttry,PID:19322 java.lang.NullPointerException:尝试调用虚方法 &#39; java.lang.String java.lang.Object.toString()&#39;在null对象上 参考 在 android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 在android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 在android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197) 在android.widget.Spinner.onMeasure(Spinner.java:507) 在android.view.View.measure(View.java:17430) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 在 android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:615) 在android.view.View.measure(View.java:17430) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.view.View.measure(View.java:17430) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 在android.view.View.measure(View.java:17430) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2560) 在android.view.View.measure(View.java:17430) 在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) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) 在android.view.Choreographer.doCallbacks(Choreographer.java:580) 在android.view.Choreographer.doFrame(Choreographer.java:550) 在 android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) 在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)
答案 0 :(得分:0)
根本原因:循环中的新String []
values=new String[jArray.length()];
答案 1 :(得分: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");
答案 2 :(得分:0)
错误是因为这些行:
values=new String[jArray.length()];
values[i]=shiva;
这是因为values数组没有填满,它给出了NullPointerException
。
尝试调试并检查jArray.length()
中的内容并确保它填满您的值数组。