前提・実現したいこと
学校の課題でAndroidStudioでメールを送信するアプリを開発しています。
画面遷移の際にクラッシュしてしまう原因が調べてもわからず質問させていただきます。
エラーメッセージ(Logcat部分)
MainActivity.java
1 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.st〇〇〇.mailer2/com.example.st〇〇〇.mailer2.SubActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 2 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2822) 3 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2897) 4 at android.app.ActivityThread.-wrap11(Unknown Source:0) 5 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1598) 6 at android.os.Handler.dispatchMessage(Handler.java:105) 7 at android.os.Looper.loop(Looper.java:251) 8 at android.app.ActivityThread.main(ActivityThread.java:6572) 9 at java.lang.reflect.Method.invoke(Native Method) 10 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 11 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 12 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 13 at com.example.st081551.mailler2.SubActivity.onCreate(SubActivity.java:40) 14 at android.app.Activity.performCreate(Activity.java:6975) 15 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 16 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2775) 17 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2897) 18 at android.app.ActivityThread.-wrap11(Unknown Source:0) 19 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1598) 20 at android.os.Handler.dispatchMessage(Handler.java:105) 21 at android.os.Looper.loop(Looper.java:251) 22 at android.app.ActivityThread.main(ActivityThread.java:6572) 23 at java.lang.reflect.Method.invoke(Native Method) 24 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 25 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
該当のソースコード
MainActivity.java
1package com.example.st〇〇〇.mailer2; 2 3import android.content.Intent; 4import android.os.Bundle; 5import android.support.v7.app.AppCompatActivity; 6import android.view.View; 7import android.widget.Button; 8import android.net.Uri; 9 10 11 12 13public class MainActivity extends AppCompatActivity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 Button button = findViewById(R.id.button); 21 button.setOnClickListener(new View.OnClickListener() { 22 @Override 23 public void onClick(View v) { 24 // mailer 呼び出し 25 syukatuMailer(); 26 } 27 }); 28 29 Button button2 = findViewById(R.id.button2); 30 button2.setOnClickListener(new View.OnClickListener() { 31 @Override 32 public void onClick(View v) { 33 // mailer 呼び出し 34 callMailer(); 35 } 36 }); 37 38 Button button3 = findViewById(R.id.button3); 39 button3.setOnClickListener(new View.OnClickListener() { 40 @Override 41 public void onClick(View v) { 42 Intent intent = new Intent(getApplication(), SubActivity.class); 43 startActivity(intent); 44 } 45 }); 46 } 47 48 49 50 String sep = System.getProperty("line.separator"); 51 52 53 private void syukatuMailer(){ 54 Intent intent = new Intent(); 55 intent.setAction(Intent.ACTION_SENDTO); 56 57 Intent st_intent = getIntent(); 58 String st_number = st_intent.getStringExtra("aaa"); 59 String st_name = st_intent.getStringExtra("bbb"); 60 61 intent.setType("text/plain"); 62 intent.setData(Uri.parse("mailto:xxx@yyy.zzz")); 63 intent.putExtra(Intent.EXTRA_SUBJECT, "タイトル"); 64 intent.putExtra(Intent.EXTRA_TEXT, "就活の連絡です。" 65 +sep 66 + st_number 67 +sep 68 + st_name ); 69 70// startActivity(intent); 71 startActivity(Intent.createChooser(intent, null)); 72 73 } 74 75 private void callMailer(){ 76 Intent intent = new Intent(); 77 intent.setAction(Intent.ACTION_SENDTO); 78 79 Intent st_intent = getIntent(); 80 String st_number = st_intent.getStringExtra("aaa"); 81 String st_name = st_intent.getStringExtra("bbb"); 82 83 intent.setType("text/plain"); 84 intent.setData(Uri.parse("mailto:xxx@yyy.zzz")); 85 intent.putExtra(Intent.EXTRA_SUBJECT, "タイトル"); 86 intent.putExtra(Intent.EXTRA_TEXT, "法事の連絡です。\r\n" + 87 "〇〇大学" + 88 "" 89 +sep 90 + st_number 91 +sep 92 +st_name ); 93 94// startActivity(intent); 95 startActivity(Intent.createChooser(intent, null)); 96 97 } 98}
SubActivity.java
1package com.example.st〇〇〇.mailer2; 2 3import android.content.Intent; 4import android.os.Bundle; 5import android.support.v7.app.AppCompatActivity; 6import android.view.View; 7import android.widget.Button; 8import android.widget.EditText; 9import android.widget.TextView; 10import android.widget.Spinner; 11 12 13 14 15 16 17public class SubActivity extends AppCompatActivity { 18 19 20 EditText editText,editText2; 21 Button okButton; 22 23 24 @Override 25 public void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_sub); 28 29 editText = findViewById(R.id.edit_st_number); 30 editText2 = findViewById(R.id.edit_st_name); 31 final Spinner school = (Spinner)this.findViewById(R.id.school_spinner); 32 33 34 35 okButton = (Button) findViewById(R.id.button3); 36 okButton.setOnClickListener(new View.OnClickListener() { 37 public void onClick(View v) { 38 39 40 String st_number = editText.getText().toString(); 41 String st_name = editText2.getText().toString(); 42 String str_school = (String)school.getSelectedItem(); 43 44 Intent st_intent = new Intent(getApplication(),MainActivity.class); 45 st_intent.putExtra("aaa",st_number); 46 st_intent.putExtra("bbb",st_name); 47 st_intent.putExtra("ccc",st_school); 48 startActivity(st_intent); 49 } 50 }); 51 52 53 54 55 /* 56 Button returnButton = findViewById(R.id.return_button); 57 returnButton.setOnClickListener(new View.OnClickListener() { 58 @Override 59 public void onClick(View v) { 60 Intent intent2 =new Intent(getApplication(),MainActivity.class); 61 intent2.putExtra("aaa",st_text); 62 startActivity(intent2); 63 } 64 }); 65 */ 66 67 68 } 69 70 71} 72 73
試したこと
AndroidManifest.xmlのチェック
遷移先のレイアウト等確認
補足情報
button3のIntent処理で怒られているのはなんとなく分かるのですが、元々このコードでエラーも出ず動いていたので何が原因か分かっていない状態です。
元はRに赤線が出まくってビルドが出来なくなったので、いろいろ試したらビルドはできるのものの遷移する際にクラッシュするという状態です。
ご教授よろしくお願いします。