0

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

3 回答 3

0

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

于 2013-08-06T01:06:56.423 回答
0

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

 setContentView(R.layout.listview);
于 2013-11-29T06:12:01.287 回答
0

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

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

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

于 2013-08-06T01:13:22.400 回答