タイトルの通り、Attempt to invoke virtual method on a null を解決したいです。
##ソースとログ
以下Logです:
java
03-22 16:09:13.525 17928-17928/com.kitunebi.sunsolararrow.flashanzan E/AndroidRuntime: FATAL EXCEPTION: main Process: com.kitunebi.sunsolararrow.flashanzan, PID: 17928 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 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5451) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 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:2139) at com.kitunebi.sunsolararrow.flashanzan.MathActivity.<init>(MathActivity.java:19) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5451) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
関係ありそうなソースコードです。
MathActivity.java
java
//パッケージを定義したりインポートするコードは省略 /** This activity sets a question. */ public class MathActivity extends Activity { /** timer. */ Timer timer; /** It displays a number. */ TextView tv; /19行目 /**The number of times change the text.*/ int kutisu; /**Max number.*/ int max; /**The speed change the text*/ int speed; /**The answer*/ int answer; /**Handler. This is used in order to change the text(UI). */ Handler mHandler = new Handler(); /**onCreate method. */ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_plusminus); this.tv=(TextView)findViewById(R.id.set); } protected void onResume(){ super.onResume(); //This code initializes kutisu, max, and speed. Intent intent = getIntent(); this.kutisu = Integer.parseInt(intent.getStringExtra("kutisu")); this.max = Integer.parseInt(intent.getStringExtra("max")); speed(Integer.parseInt(intent.getStringExtra("speed"))); startTimer();//Start a timer //Start a new activity intent.setClassName(this,"AnswerActivity"); intent.putExtra("answer", answer); startActivity(intent); } //speedメソッドやstartTimerメソッド。省略 ・ ・ ・ }
レイアウト
xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="願いましては" android:id="@+id/set" <!--定義しているのですが---> android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:textSize="50dp" /> </RelativeLayout>
android:id="@+id/set"
と書いたのですが、エラーが出ています。また、これを見ましたが原因が違いそうです。(もしも原因が一緒ならすみません)
まだ回答がついていません
会員登録して回答してみよう