我正在尝试使用意图导航到多个活动,但我遇到了运行时异常,我是 eclipse 的初学者,所以请指导我

Posted

技术标签:

【中文标题】我正在尝试使用意图导航到多个活动,但我遇到了运行时异常,我是 eclipse 的初学者,所以请指导我【英文标题】:I am trying to navigate to multiple activities using intents and i am getting a run time exception, I am a beginner with eclipse , so please guide me 【发布时间】:2016-01-28 17:53:22 【问题描述】:

MainActivity.java

public class MainActivity extends Activity 
  @Override
  protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText txtUserName = (EditText)findViewById(R.id.editText1);
    final EditText txtPassword = (EditText)findViewById(R.id.editText2);
    Button btnLogin = (Button)findViewById(R.id.button1);
    btnLogin.setOnClickListener(new OnClickListener()
       @Override
       public void onClick(View v) 
         String username = txtUserName.getText().toString();
         String password = txtPassword.getText().toString();
         String foo = new String("131031001");
         String foo2 = new String("131031002");
         String foo3 = new String("131031003");
         String foo4 = new String("131031004");
         String foo5 = new String("131031005");
         try
           if(username.length() > 0 && password.length() >0) 
             DBUserAdapter dbUser = new DBUserAdapter(MainActivity.this);
             dbUser.open();
             dbUser.AddUser();
             if(dbUser.Login(username, password))
             
                Toast.makeText(MainActivity.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
                if(username.equals(foo))
                
                   Intent nextActivity2 = new Intent(getApplicationContext(),SecondActivity.class);
                   nextActivity2.putExtra("Second",username);
                   startActivity(nextActivity2);
                
                else if(username.equals(foo2)) 
                   Intent nextActivity3 = new Intent(getBaseContext(),ThirdActivity.class);
                    nextActivity3.putExtra("Third",username);
                    startActivity(nextActivity3);
                
                else if(username.equals(foo3))
                
                   Intent nextActivity4 = new Intent(getBaseContext(),FourthActivity.class);
                   nextActivity4.putExtra("Fourth",username);
                   startActivity(nextActivity4);
                
                else if(username.equals(foo4)) 
                   Intent nextActivity5 = new Intent(getBaseContext(),FifthActivity.class);
                   nextActivity5.putExtra("Fifth",username);
                   startActivity(nextActivity5);
                
                else if(username.equals(foo5)) 
                   Intent nextActivity6 = new Intent(getBaseContext(),SixthActivity.class);
                   nextActivity6.putExtra("Sixth",username);
                   startActivity(nextActivity6);
                
                else 
                   Toast.makeText(MainActivity.this,"Result does not exist! Try later!!", Toast.LENGTH_LONG).show();
                
            else 
                Toast.makeText(MainActivity.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
            
            dbUser.close();
          

        catch(Exception e) 
          Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
        
      

    );

    Button btreg = (Button)findViewById(R.id.button2);
    btreg.setOnClickListener(new OnClickListener()
       @Override
       public void onClick(View v) 
          Intent nextActivity = new Intent(getApplicationContext(),Register.class);
          startActivity(nextActivity);
       
     );
   

SecondActivity.java

public class SecondActivity extends Activity   
   Intent intent = getIntent();
   @Override
   protected void onCreate(Bundle savedInstanceState) 
     super.onCreate(savedInstanceState);
     setContentView(R.layout.second_activity);
   

所有其他活动的编码与上述类似。 这是我的日志。

10-28 14:53:10.688: E/androidRuntime(1196): Process: com.skcetresults, PID: 1196
10-28 14:53:10.688: E/AndroidRuntime(1196): java.lang.RuntimeException: Unable to start activity        ComponentInfocom.skcetresults/com.skcetresults.SecondActivity: java.lang.NullPointerException
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.os.Looper.loop(Looper.java:137)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.app.ActivityThread.main(ActivityThread.java:4998)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at java.lang.reflect.Method.invoke(Method.java:515)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at dalvik.system.NativeStart.main(Native Method)
10-28 14:53:10.688: E/AndroidRuntime(1196): Caused by: java.lang.NullPointerException
10-28 14:53:10.688: E/AndroidRuntime(1196):     at com.skcetresults.SecondActivity.onCreate(SecondActivity.java:15)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.app.Activity.performCreate(Activity.java:5243)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-28 14:53:10.688: E/AndroidRuntime(1196):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
10-28 14:53:10.688: E/AndroidRuntime(1196):     ... 11 more

只有两个意图,代码运行良好,但是当我将值与字符串进行比较时,应用程序崩溃了,我不知道错误在哪里。

【问题讨论】:

Intent intent = getIntent(); 放入 onCreate() 方法中。 SecondActivity 中的第 15 行是什么? 【参考方案1】:

使用 MainActivity.this 作为上下文,而不是 getBaseContext() 和 getApplicationContext(),可以在任何地方工作。

【讨论】:

【参考方案2】:

你应该这样写

public class SecondActivity extends Activity   

@Override
protected void onCreate(Bundle savedInstanceState) 
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);

Intent i = getIntent();


如果额外的数据是字符串,你可以像这样得到它:

String nameString = i.getStringExtra("nameExtra");

【讨论】:

感谢您的输入,但是当我更改为 MainActivity.this 而不是 getBaseContext() 和 getApplicationContext() 时应用程序开始工作【参考方案3】:

您不能在 onCreate() 之前使用 getIntent(),此时根本没有可用的 Intent。

在您的代码中,我怀疑您正在尝试获取StringExtras from Intent,但由于intent 为空,它正在抛出NullPointerException

在您的Second Activity 中,您可以在onCreate() 方法中使用getIntent()

public class SecondActivity extends Activity   
    Intent intent;   
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        intent = getIntent();
        String userName = intent.getStringExtra("Second");
        ...
     

【讨论】:

感谢您的输入,但是当我更改为 MainActivity.this 而不是 getBaseContext() 和 getApplicationContext() 时应用程序开始工作【参考方案4】:

使用少量代码检查,以确保您的意图行为正常。例如,您可以用if 语句包围您的getIntent 代码。对于调用哪个正确的方法,您必须四处逛逛,因为这取决于您希望您的应用程序做什么。幸运的是,当您写下一些 Android 资源时,Eclipse 会为您提供所有相关文档,例如Intent

【讨论】:

感谢您的输入,但是当我更改为 MainActivity.this 而不是 getBaseContext() 和 getApplicationContext() 时应用程序开始工作

以上是关于我正在尝试使用意图导航到多个活动,但我遇到了运行时异常,我是 eclipse 的初学者,所以请指导我的主要内容,如果未能解决你的问题,请参考以下文章

Appium给出'错误:运行测试时意图匹配多个活动

向上导航的空指针android

帮助文件中意图和活动导航的 AtomicReferences

使用具有不同活动和最终电子邮件意图的多个意图

带有多个 if 语句的意图

如何从我的活动中意图在我的标签栏中分段?