-1

コードに問題があります。タイトル「新規プロファイル」をクリックすると、新しいアクティビティが開始されますが、をクリックすると、「残念ながら%AppName%は停止しました。」というアラートが表示されます。ここでIntentコマンドでAndroidエラーが発生しました

は、私は私が間違っているのものを見つけることができません

public class NewProfileActivity extends AppCompatActivity { 

     EditText profile, password, confirmpass; 
     Intent insert = new Intent(NewProfileActivity.this, MainActivity.class); 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_new_profile); 
     } 

     public void backMain(View v){ 

      startActivity(back); 

     } 

     public void insertData(View v) { 

      profile = (EditText) findViewById(R.id.profileName); 
      password = (EditText) findViewById(R.id.password); 
      confirmpass = (EditText) findViewById(R.id.confirmPassword); 

      insert.putExtra("profile", profile.getText().toString()); 
      insert.putExtra("password", password.getText().toString()); 
      insert.putExtra("profile", confirmpass.getText().toString()); 

      compare(password.getText().toString(), confirmpass.getText().toString()); 
     } 

     public void compare(String x, String y){ 

      final AlertDialog alert = new AlertDialog.Builder(NewProfileActivity.this).create(); 

      if(x.equals(y)){ 
       alert.setTitle("New Profile!"); 
       alert.setMessage("Profile "+profile+" registered"); 
       alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss(); 
          } 
         } 
       ); 
       startActivity(insert); 
      } else { 
       alert.setTitle("Ops..."); 
       alert.setMessage("Passwords don't macth"); 
       alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss(); 
          } 
         } 
       ); 
      } 
     } 
    } 

のAndroidモニターのログ

--------- beginning of crash 
05-17 01:19:54.545 2568-2568/com.example.felipe.myappproject E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.felipe.myappproject, PID: 2568 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.felipe.myappproject/com.example.felipe.myappproject.NewProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     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 'java.lang.String android.content.Context.getPackageName()' on a null object reference 
     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133) 
     at android.content.ComponentName.<init>(ComponentName.java:128) 
     at android.content.Intent.<init>(Intent.java:4449) 
     at com.example.felipe.myappproject.NewProfileActivity.<init>(NewProfileActivity.java:14) 
     at java.lang.Class.newInstance(Native Method) 
     at android.app.Instrumentation.newActivity(Instrumentation.java:1067) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
     at android.app.ActivityThread.-wrap11(ActivityThread.java)  
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
     at android.os.Handler.dispatchMessage(Handler.java:102)  
     at android.os.Looper.loop(Looper.java:148)  
     at android.app.ActivityThread.main(ActivityThread.java:5417)  
     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)  

そして、ここに私のNewProfileActivity.javaです。 "public void backMain(View v)"以外のすべてを削除してコードを単純化しようとしましたが、同じ問題が発生します。

+1

そこに 'Intent'sをインスタンス化することはできません。自分のメソッドで必要とする直前に作成してください。 –

+0

コードは@MikeMで動作します。の決断。ありがとう、私はインテントが私の方法の外にあったことを認識しません。 – felipe218

答えて

0

@ MikeMのコメントによれば、パラメータとして渡すクラスさえまだ完全には初期化されていないところで、Intentを最初から初期化しています。

あなたがそうのようにそれを使用する前に、あなたは、単にちょうど初期化する必要があります。

public class NewProfileActivity extends AppCompatActivity { 

    EditText profile, password, confirmpass; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_new_profile); 
    } 

    public void backMain(View v) { 
     // startActivity(back); 
    } 

    public void insertData(View v) { 

     profile = (EditText) findViewById(R.id.profileName); 
     password = (EditText) findViewById(R.id.password); 
     confirmpass = (EditText) findViewById(R.id.confirmPassword); 

     compare(password.getText().toString(), confirmpass.getText().toString()); 
    } 

    public void compare(String x, String y) { 

     final AlertDialog alert = new AlertDialog.Builder(NewProfileActivity.this).create(); 

     if (x.equals(y)) { 
      alert.setTitle("New Profile!"); 
      alert.setMessage("Profile " + profile + " registered"); 
      alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
         } 
        } 
      ); 
      // CODES Transferred here ==== 
      Intent insert = new Intent(NewProfileActivity.this, MainActivity.class); 

      insert.putExtra("profile", profile.getText().toString()); 
      insert.putExtra("password", password.getText().toString()); 
      insert.putExtra("profile", confirmpass.getText().toString()); 
      // CODES Transferred here ==== 
      startActivity(insert); 
     } else { 
      alert.setTitle("Ops..."); 
      alert.setMessage("Passwords don't macth"); 
      alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
         } 
        } 
      ); 
     } 
    } 
} 

乾杯! :D

関連する問題