当我运行我的程序时它崩溃并且我的 logcat 说“无法实例化活动”。
现在我知道你在想它说我没有在我的清单中写我的活动,但它是不同的东西。
我真的不知道如何解释或修复它,所以我只是让你们看看它,希望你们能告诉我发生了什么。如果你还想看我的清单,也请告诉我。
08-05 22:53:33.356: E/AndroidRuntime(793): FATAL EXCEPTION: main
08-05 22:53:33.356: E/AndroidRuntime(793): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.theproblemsolver/com.theproblemsolver.ListView}: java.lang.NullPointerException
08-05 22:53:33.356: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-05 22:53:33.356: E/AndroidRuntime(793): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-05 22:53:33.356: E/AndroidRuntime(793): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-05 22:53:33.356: E/AndroidRuntime(793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-05 22:53:33.356: E/AndroidRuntime(793): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 22:53:33.356: E/AndroidRuntime(793): at android.os.Looper.loop(Looper.java:137)
08-05 22:53:33.356: E/AndroidRuntime(793): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-05 22:53:33.356: E/AndroidRuntime(793): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 22:53:33.356: E/AndroidRuntime(793): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 22:53:33.356: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-05 22:53:33.356: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-05 22:53:33.356: E/AndroidRuntime(793): at dalvik.system.NativeStart.main(Native Method)
08-05 22:53:33.356: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
08-05 22:53:33.356: E/AndroidRuntime(793): at android.app.Activity.findViewById(Activity.java:1839)
08-05 22:53:33.356: E/AndroidRuntime(793): at com.theproblemsolver.ListView.<init>(ListView.java:46)
08-05 22:53:33.356: E/AndroidRuntime(793): at java.lang.Class.newInstanceImpl(Native Method)
08-05 22:53:33.356: E/AndroidRuntime(793): at java.lang.Class.newInstance(Class.java:1319)
08-05 22:53:33.356: E/AndroidRuntime(793): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-05 22:53:33.356: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-05 22:53:33.356: E/AndroidRuntime(793): ... 11 more
主要活动:
public class MainActivity extends Activity {
EditText et;
Button getanswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et = (EditText) findViewById(R.id.editText1);
Button getanswer = (Button) findViewById(R.id.button1);
getanswer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ListView.class);
startActivity(i);
}
});
}
}
列表视图活动:
public class ListView extends ListActivity {
private ProgressDialog pDialog;
JSONParsser jParser = new JSONParsser();
ArrayList<HashMap<String, String>> questionList;
//JSONObject json = jParser.getJSONFromURI(URI);
final String TAG_RESULTS = "results";
final String TAG_QUESTION_SUBJECT = "Subject";
final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
final String TAG_QUESTION = "Question";
final String TAG_QUESTION_CONTENT = "Content";
final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
final String TAG_ANSWERS = "Answers";
final String TAG_ANSWER = "Answer";
final String TAG_ANSWERS_CONTENT = "Content";
JSONArray results;
EditText et = (EditText) findViewById(R.id.editText1);
final String searchTerm = et.getText().toString().trim();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.listview);
//questionList = new ArrayList<HashMap<String, String>>();
new LoadAllData().execute();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
startActivity(intent);
finish();
}
}
class LoadAllData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListView.this);
pDialog.setMessage("Loading Data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
if (pDialog != null && pDialog.isShowing()) pDialog.dismiss();
}
protected String doInBackground(String... args) {
try {
String query = URLEncoder.encode(searchTerm, "utf-8");
String URL = "http://example.com/json";
JSONObject jObj = jParser.readJSONFeed(URL);
try {
results = jObj.getJSONArray(TAG_RESULTS);
for(int i = 0; i < results.length(); i++) {
JSONObject r = results.getJSONObject(i);
JSONObject Question = r.getJSONObject(TAG_QUESTION);
String Subject = Question.getString(TAG_QUESTION_SUBJECT);
String NumAnswers = Question.getString(TAG_QUESTION_NUMANSWERS);
String ChosenAnswers= Question.getString(TAG_QUESTION_CHOSENANSWER);
String Content = Question.getString(TAG_QUESTION_CONTENT);
JSONObject Answers = Question.getJSONObject(TAG_ANSWERS);
JSONObject Answer = Answers.getJSONObject(TAG_ANSWER);
String Content1 = Answers.getString(TAG_ANSWERS_CONTENT);
questionList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_QUESTION_SUBJECT, Subject);
map.put(TAG_QUESTION_NUMANSWERS, NumAnswers);
questionList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TAG_RESULTS ;
}
@Override
protected void onPostExecute(String file_URL) {
ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
R.layout.listview,
new String[] { TAG_QUESTION_SUBJECT, TAG_QUESTION_NUMANSWERS }, new int[] {
R.id.Subject, R.id.NumAnswers });
setListAdapter(adapter);
android.widget.ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
}
}
}
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theproblemsolver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.theproblemsolver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ListView"
android:label="@string/app_name" >
</activity>
</application>