我在网上遵循了教程。但是我无法使其正常工作,并且不断收到NullPointerException。该应用程序花费大约2-3分钟尝试连接到我的服务器,然后才关闭。
我认为问题出在阵列适配器中。
数组适配器有13个对象,但将其设置为微调器时显示为null请帮助我解决我是Android新手的问题,因此需要一些帮助。
我的代码MainActivity
public class MainActivity extends Activity {
Spinner SID;
Spinner SName;
Spinner SMailID;
Button Btngetdata;
ArrayList<String> Name = new ArrayList<String>();
List<String> UID = new ArrayList<String>();
List<String> MailID = new ArrayList<String>();
//URL to get JSON Array
private static String url = "http://api.androidhive.info/contacts/";
//JSON Node Names
private static final String TAG_USER = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
JSONArray user = null;
@SuppressWarnings("unused")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner SID = (Spinner) findViewById(R.id.listID);
Spinner SName = (Spinner) findViewById(R.id.listName);
Spinner SMailID = (Spinner) findViewById(R.id.listMailID);
Btngetdata = (Button)findViewById(R.id.btngetData);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
if(json != null)
{
try {
// Getting JSON Array
user = json.getJSONArray(TAG_USER);
JSONObject c = user.getJSONObject(0);
for (int i = 0; i < user.length(); i++) {
UID.add(user.getJSONObject(i).getString(TAG_ID.toString()));
Name.add(user.getJSONObject(i).getString(TAG_NAME.toString()));
MailID.add(user.getJSONObject(i).getString(TAG_EMAIL.toString()));
}
//Set JSON Data in TextView
ArrayAdapter<String> IDAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, UID);
IDAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SID.setAdapter(IDAdapter);
ArrayAdapter<String> NameSource = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, Name);
NameSource.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SName.setAdapter(NameSource);
ArrayAdapter<String> MailIDSource = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, MailID);
MailIDSource.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SMailID.setAdapter(MailIDSource);
} catch (JSONException e) {
e.printStackTrace();
}
}
else
{
AlertDialog alertDialog = new AlertDialog.Builder(
MainActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(null);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}
}
}
}
这是我得到的错误
11-26 23:38:06.038: E/AndroidRuntime(26316): FATAL EXCEPTION: main
11-26 23:38:06.038: E/AndroidRuntime(26316): java.lang.NullPointerException
11-26 23:38:06.038: E/AndroidRuntime(26316): at learn2crack.asynctask.MainActivity$JSONParse.onPostExecute(MainActivity.java:122)
11-26 23:38:06.038: E/AndroidRuntime(26316): at learn2crack.asynctask.MainActivity$JSONParse.onPostExecute(MainActivity.java:1)
11-26 23:38:06.038: E/AndroidRuntime(26316): at android.os.AsyncTask.finish(AsyncTask.java:631)
11-26 23:38:06.038: E/AndroidRuntime(26316): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-26 23:38:06.038: E/AndroidRuntime(26316): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-26 23:38:06.038: E/AndroidRuntime(26316): at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 23:38:06.038: E/AndroidRuntime(26316): at android.os.Looper.loop(Looper.java:137)
11-26 23:38:06.038: E/AndroidRuntime(26316): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-26 23:38:06.038: E/AndroidRuntime(26316): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 23:38:06.038: E/AndroidRuntime(26316): at java.lang.reflect.Method.invoke(Method.java:511)
11-26 23:38:06.038: E/AndroidRuntime(26316): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-26 23:38:06.038: E/AndroidRuntime(26316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-26 23:38:06.038: E/AndroidRuntime(26316): at dalvik.system.NativeStart.main(Native Method)
最佳答案
您已经声明了
public class MainActivity extends Activity {
Spinner SID;
Spinner SName;
Spinner SMailID;
所以改变这个
Spinner SID = (Spinner) findViewById(R.id.listID);
Spinner SName = (Spinner) findViewById(R.id.listName);
Spinner SMailID = (Spinner) findViewById(R.id.listMailID);
// becomes local to onCreate.
至
SID = (Spinner) findViewById(R.id.listID);
SName = (Spinner) findViewById(R.id.listName);
SMailID = (Spinner) findViewById(R.id.listMailID);