タイトルの通り、Attempt to invoke virtual method on a null を解決したいです。
##ソースとログ
以下Logです:
java
103-22 16:09:13.525 17928-17928/com.kitunebi.sunsolararrow.flashanzan E/AndroidRuntime: FATAL EXCEPTION: main 2 Process: com.kitunebi.sunsolararrow.flashanzan, PID: 17928 3 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kitunebi.sunsolararrow.flashanzan/com.kitunebi.sunsolararrow.flashanzan.MathActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 4 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345) 5 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 6 at android.app.ActivityThread.access$900(ActivityThread.java:153) 7 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 8 at android.os.Handler.dispatchMessage(Handler.java:102) 9 at android.os.Looper.loop(Looper.java:148) 10 at android.app.ActivityThread.main(ActivityThread.java:5451) 11 at java.lang.reflect.Method.invoke(Native Method) 12 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 13 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 14 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 15 at android.app.Activity.findViewById(Activity.java:2139) 16 at com.kitunebi.sunsolararrow.flashanzan.MathActivity.<init>(MathActivity.java:19) 17 at java.lang.Class.newInstance(Native Method) 18 at android.app.Instrumentation.newActivity(Instrumentation.java:1068) 19 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335) 20 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 21 at android.app.ActivityThread.access$900(ActivityThread.java:153) 22 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 23 at android.os.Handler.dispatchMessage(Handler.java:102) 24 at android.os.Looper.loop(Looper.java:148) 25 at android.app.ActivityThread.main(ActivityThread.java:5451) 26 at java.lang.reflect.Method.invoke(Native Method) 27 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 28 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 29
関係ありそうなソースコードです。
MathActivity.java
java
1//パッケージを定義したりインポートするコードは省略 2 3/** This activity sets a question. */ 4public class MathActivity extends Activity { 5 /** timer. */ 6 Timer timer; 7 /** It displays a number. */ 8 TextView tv; /19行目 9 /**The number of times change the text.*/ 10 int kutisu; 11 /**Max number.*/ 12 int max; 13 /**The speed change the text*/ 14 int speed; 15 /**The answer*/ 16 int answer; 17 /**Handler. This is used in order to change the text(UI). */ 18 Handler mHandler = new Handler(); 19 /**onCreate method. */ 20 @Override 21 protected void onCreate(Bundle savedInstanceState){ 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_plusminus); 24 this.tv=(TextView)findViewById(R.id.set); 25 } 26 protected void onResume(){ 27 super.onResume(); 28 29 //This code initializes kutisu, max, and speed. 30 Intent intent = getIntent(); 31 this.kutisu = Integer.parseInt(intent.getStringExtra("kutisu")); 32 this.max = Integer.parseInt(intent.getStringExtra("max")); 33 speed(Integer.parseInt(intent.getStringExtra("speed"))); 34 35 startTimer();//Start a timer 36 37 //Start a new activity 38 intent.setClassName(this,"AnswerActivity"); 39 intent.putExtra("answer", answer); 40 startActivity(intent); 41 } 42 //speedメソッドやstartTimerメソッド。省略 43 ・ 44 ・ 45 ・ 46}
レイアウト
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" android:layout_height="match_parent"> 4 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:textAppearance="?android:attr/textAppearanceLarge" 9 android:text="願いましては" 10 android:id="@+id/set" 11 <!--定義しているのですが---> 12 android:layout_centerVertical="true" 13 android:layout_centerHorizontal="true" 14 android:textSize="50dp" /> 15</RelativeLayout>
android:id="@+id/set"
と書いたのですが、エラーが出ています。また、これを見ましたが原因が違いそうです。(もしも原因が一緒ならすみません)
回答3件
あなたの回答
tips
プレビュー