2015-01-03 21 views
-3

我是新來的Android,我試圖運行一個應用程序(簡單的測試應用程序),但得到這個錯誤(我已經註冊了我在AndroidManifest.xml中的所有活動,我使用的是Android 1.0.2工作室java.lang.RuntimeException:無法實例化活動ComponentInfo,所有活動都在AndroidManifest.xml中註冊,不知道什麼是錯的

下面是一個應用程序的logcat的:

MainActivity

01-03 20:34:24.128 17300-17300/com.example.temur.sherlocked D/AndroidRuntime﹕ Shutting down VM 
01-03 20:34:24.134 17300-17300/com.example.temur.sherlocked E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.temur.sherlocked, PID: 17300 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.temur.sherlocked/com.example.temur.sherlocked.MainActivity}: java.lang.InstantiationException: com.example.temur.sherlocked.MainActivity 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
      at android.app.ActivityThread.access$800(ActivityThread.java:139) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5086) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    Caused by: java.lang.InstantiationException: com.example.temur.sherlocked.MainActivity 
      at java.lang.Class.newInstance(Class.java:1561) 
      at android.app.Instrumentation.newActivity(Instrumentation.java:1084) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
            at android.app.ActivityThread.access$800(ActivityThread.java:139) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5086) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Constructor.newInstance(Native Method) 
      at java.lang.Class.newInstance(Class.java:1559) 
            at android.app.Instrumentation.newActivity(Instrumentation.java:1084) 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
            at android.app.ActivityThread.access$800(ActivityThread.java:139) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5086) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.Activity.findViewById(Activity.java:1892) 
      at com.example.temur.sherlocked.MainActivity.<init>(MainActivity.java:43) 
            at java.lang.reflect.Constructor.newInstance(Native Method) 
            at java.lang.Class.newInstance(Class.java:1559) 
            at android.app.Instrumentation.newActivity(Instrumentation.java:1084) 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
            at android.app.ActivityThread.access$800(ActivityThread.java:139) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5086) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 

代碼

package com.example.temur.sherlocked; 

    import android.content.Intent; 
    import android.support.v7.app.ActionBarActivity; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.TextView; 


    public class MainActivity extends ActionBarActivity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
     } 


     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_main, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 

      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 

     TextView infoTextView = (TextView)findViewById(R.id.textViewAnswer); 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data){ 

      super.onActivityResult(requestCode,resultCode,data); 

      if (requestCode==CHOOSE_THIEF){ 
       if (resultCode==RESULT_OK){ 
        String thiefName = data.getStringExtra(SecondActivity.THIEF); 
        infoTextView.setText(thiefName); 
       }else { 
        infoTextView.setText(""); 
       } 
      } 
     } 
    static final private int CHOOSE_THIEF = 0; 


    public void onClick(View view) { 
     Intent questionIntent = new Intent(MainActivity.this, SecondActivity.class); 
     startActivityForResult(questionIntent,CHOOSE_THIEF); 
    } 
} 

請幫助我:)

+0

主要原因是'NullPointerException:試圖調用虛擬方法'android.view.View android.view.Window.findViewById(int)'在空對象引用上。搜索一下,你可能會找到答案。 – Tom

+0

你還沒有安裝'setContentView(R.layout.activity);''onCreate'方法'' – SorryForMyEnglish

+0

發佈你的代碼太多了......但很難知道問題 – Psypher

回答

2

如果您閱讀了其他堆棧溢出問題,您會注意到,除了堆棧跟蹤外,寫得很好的問題還包括崩潰的源代碼(在您的情況下爲MainActivity)。

在這種情況下,我可以從堆棧跟蹤中知道您在MainActivity上過早呼叫findViewById()。直到您的電話setContentView()才能完成。

尤其是,您似乎在數據成員的初始化程序中調用findViewById()。除非另有特別指示,否則之前的不是呼叫繼承方法,除非super.onCreate()

+0

感謝你的評論,爲MainActivity添加了代碼 –

+0

@temur_p :根據我的回答,在'setContentView()'調用之後,將'infoTextView =(TextView)findViewById(R.id.textViewAnswer);'放在'onCreate()'方法內。 – CommonsWare

+0

謝謝,你真的幫了忙。我的一個愚蠢的錯誤(( –

相關問題