2013-08-06 46 views
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 

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> 

回答

0

嘗試從包名稱刪除/,Eclipse不一樣,一般。發佈Manifest會很有幫助。沒有做AsyncTasks程序是否正確運行?取消註釋setContentView(...)。這應該是所有的東西,以更好地瞭解你的問題。另外,使用谷歌作爲你的主要問題解決者。有很多已經類似的問題,它們具有相同的包名稱「com.theproblemsolver.listview」,也使用類似的方法。

0

你的問題是在這裏,在你的ListView活動類(可能不是一個好主意來命名這種方式,將只創建了Android的ListView控件類混淆):

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

有沒有這樣的觀在ListActivity附帶的默認佈局中。即使您使用這種View提供自定義佈局,在調用setContentView()之前,findViewById()也會失敗。

0

嘗試去掉這一行,因爲幾乎所有的活動,與用戶進行交互,所以Activity類以創爲您窗口,在其中您可以將使用的setContentView(查看)UI的照顧。所以下面嘗試..

setContentView(R.layout.listview); 
相關問題