質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

3回答

23616閲覧

Attempt to invoke virtual method on a null を解決したい

sun-solar-arrow

総合スコア113

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2017/03/22 07:46

編集2017/03/23 00:25

タイトルの通り、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メソッド。省略 43444546}

レイアウト

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"と書いたのですが、エラーが出ています。また、これを見ましたが原因が違いそうです。(もしも原因が一緒ならすみません)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

swordone

2017/03/22 15:21

このエラーが出た時点でのMathActivityの19行目は、onCreateの中のfindViewByIdで間違いないですか?
uniko

2017/03/23 03:50

リソース周りの問題ならClean buildあたりで直るって事はないですかね?
guest

回答3

0

findViewById(int)
あたりでエラーになっているので

startActivity(intent);

これで飛んだ先でもMathActivityのtvを表示させようとしているとか...

投稿2017/03/25 08:32

aja

総合スコア3733

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

自己解決

すみません、ビルドし直したら何故か出なくなりました。
前のビルドがうまくできてなかったのかな。。。
あと、回答を投稿してなくてすみません。

投稿2017/03/30 07:51

sun-solar-arrow

総合スコア113

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

uniko

2017/03/30 09:07

R側が更新されてなかったのだと思われ。対応するIDがずれてる状態なのでコンパイルは通ります。そしてよくあります。 レイアウトの順番変えたりするだけでも起きる場合あるのでなんかリソース関連で落ちたらとりあえずビルドし直しは覚えておいたほうがいいです。
guest

0

もしかして、と思って書きますが、

setContentView(R.layout.activity_plusminus); this.tv=(TextView)findViewById(R.id.set);

R.id.setで読んでいるTextViewは、activity_plusminusの中で定義しているものでしょうか?
他のレイアウトファイルで定義しているということはないでしょうか。

投稿2017/03/30 01:40

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問