2017-05-09 71 views
-8

當我運行我的應用程序出錯了 ,我得到這個錯誤:E/AndroidRuntime:致命異常:主要[DUPLI]

05-09 09:23:21.215 9003-9003/? I/art: Late-enabling -Xcheck:jni 05-09 09:23:21.544 9003-9003/com.example.aryan.lb_minisquad_re2 D/AndroidRuntime: Shutting down VM 05-09 09:23:21.545 9003-9003/com.example.aryan.lb_minisquad_re2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aryan.lb_minisquad_re2, PID: 9003 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.aryan.lb_minisquad_re2/com.example.aryan.lb_minisquad_re2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 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:2072) at com.example.aryan.lb_minisquad_re2.MainActivity.(MainActivity.java:19) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1606) at android.app.Instrumentation.newActivity(Instrumentation.java:1066) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  05-09 09:23:24.923 9003-9003/com.example.aryan.lb_minisquad_re2 I/Process: Sending signal. PID: 9003 SIG: 9

這是我的MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) //TODO solve runtime problem(E/AndroidRuntime: FATAL EXCEPTION: main) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

} 
TextView tvnum = (TextView) findViewById(R.id.TV_num);//creating object of TextView 
public int id = 100;//id is first number of tv num 
Button btnMin = (Button) findViewById(R.id.btnMIN); //creating object of btnMin to avoid of exceptions 
    public void onbtnMinClicked()//calling onClick listener of btnMin 
    { 
     String num = tvnum.getText().toString();//num is value of tvnum 
     num = String.valueOf(--id);//setting num one less than self(what btnmin have to do 
     tvnum.setText(num);//setting num again to the tvnum 
    } 
} 
+2

那麼,出了點問題。 – ThingyWotsit

+0

沒有堆棧跟蹤和代碼就無法解決這個問題。所以除非你提供,正如ThingyWotsit所說,出現了一些問題 – Zoe

+0

'我得到這個錯誤'哪個錯誤? – greenapps

回答

1

您實例化必須在方法內部檢查以下並在setContentView方法下

TextView tvnum =null; 
Button btnMin=null; 
@Override 
protected void onCreate(Bundle savedInstanceState) //TODO solve runtime problem(E/AndroidRuntime: FATAL EXCEPTION: main) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
tvnum = (TextView) findViewById(R.id.TV_num);//creating object of TextView 
int id = 100;//id is first number of tv num 
btnMin = (Button) findViewById(R.id.btnMIN); 
} 
+0

所以我不能讓我的變量在onCreate方法中公開,並且我不能在onClick方法中使用它。 –

+0

檢查我更新的答案,這是如何讓他們公開的 所以你可以在課堂內任何你想要的地方使用它們 –