一个不同的“无法实例化活动”错误

时间:2013-08-06 00:24:55

标签: android android-listview

当我运行我的程序时,它会崩溃并且我的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

MainActivity:

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);
            }

        });
    }
}

ListViewActivity:

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>

3 个答案:

答案 0 :(得分:0)

尝试从包名中删除/,Eclipse通常不喜欢这样。发布清单会有所帮助。没有AsyncTasks,程序是否正确运行?取消注释setContentView(...)。这应该是所有能够更好地了解您的问题的东西。此外,使用谷歌作为您的主要问题解决者。有许多已经相似的问题具有相同的包名“com.theproblemsolver.listview”,并且也使用类似的方法。

答案 1 :(得分:0)

你的问题就在这里,在你的ListView活动类中(以这种方式命名可能不是一个好主意,只会与Android ListView小部件类产生混淆):

EditText et = (EditText) findViewById(R.id.editText1);

ListActivity附带的默认布局中没有此类视图。即使您提供具有此类View的自定义布局,findViewById()也会在调用setContentView()之前失败。

答案 2 :(得分:0)

尝试取消注释此行,因为几乎所有活动都与用户交互,因此Activity类负责为您创建一个窗口,您可以在其中使用setContentView(View)放置UI。所以请尝试以下..

 setContentView(R.layout.listview);