在尝试启动活动时出现崩溃

时间:2017-01-03 22:41:41

标签: java android android-activity android-fragmentactivity

应用程序中几乎所有当前导航都工作得非常好,并且在为我导航的其他部分执行相同的操作时。

尝试从一个活动导航到另一个活动时出现此错误:

FATAL EXCEPTION: main
              Process: com.package, PID: 15338
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package/ui.activity.AccountActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                  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 'int android.view.View.getId()' on a null object reference
                  at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:181)
                  at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:174)
                  at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:160)
                  at ui.activity.AccountActivity.setInitialFragment(AccountActivity.java:38)
                  at ui.activity.BaseActivity.onCreate(BaseActivity.java:56)
                  at android.app.Activity.performCreate(Activity.java:6237)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                  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) 

我可以跟踪Null引用的位置,并尝试了一些尝试并绕过它但它们都会使应用程序崩溃。

以下是我发起活动的块:

@Override
public void onMyAccountOptionSelected() {
    Intent intent = new Intent(this, AccountActivity.class);
    startActivity(intent);
}

然后它会在view.getId()上被捕到,这个是在baseActivity:

private void setInitialFragment(View view, Fragment fragment) {
    if (this.getCurrentFragment() == null) {
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(view.getId(), fragment).commit();
    }
}

我最初在AccountActivity中调用setInitialFragment()来扩展我的baseActivity:

 @Override
protected void setInitialFragment() {

    fragment = (AccountFragment) AccountFragment.newInstance();
    setInitialFragment(fragment);

}

正如您在上面看到的那样,现在有一个AccountFragment,在这个类中,我从一个abstractFragment扩展并有一个方法来声明一个newInstance:

public static Fragment newInstance() {

    AccountFragment fragment = new AccountFragment();
    return fragment;
}

我无法找到可能影响导航到此活动的过程的任何其他地方。我希望找到一种方法来快速修复或找到更合适的方法来解决这个问题。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

您没有向我们提供真正有用的所有信息。

我想象你的AccountActivity中的setInitialFragment会是这样的:

public class AccountActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.container);
        AccountFragment fragment = AccountFragment.getInstance(); 
        setInitialFragment(fragment);
    } 

}

根据您发布的内容,您正在调用两种不同的方法:

setInitialFragment(Fragment fragment);

与此

不同
setInitialFragment(View view, Fragment fragment)