活动通过意图启动而崩溃,但在设置为主活动时则不会

时间:2012-11-09 23:53:55

标签: android android-intent android-fragments

所以我有一个FragmentActivity,但我无法切换到它。如果它是我的应用程序主要活动,它可以正常工作,但是当我尝试切换到它时它没有。

public class SomeFragmentActivity extends FragmentActivity implements AnimationLayout.Listener,
    OnClickListener {

public int mPage = 1;

protected ListView mList;
protected AnimationLayout mLayout;
protected String[] mStrings = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_content_layout);

    findViewById(R.id.sidebar_button).setOnClickListener(this);

    mLayout = (AnimationLayout) findViewById(R.id.animation_layout);
    mLayout.setListener(this);

    mList = (ListView) findViewById(R.id.sidebar_list);
    mList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, mStrings));
    mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


}

在启动该片段的主要活动的onCreate中:

public void initialize() {
        mActivity.setContentView(R.layout.activity_main_layout);

        ((Button) findViewById(R.id.main_activity_button_some_list))
                .setOnClickListener(this);
        ((Button) findViewById(R.id.main_activity_button_fragment))
                .setOnClickListener(this);


    }

是onCreate,并且:

public void onClick(View v) {
    Intent intent = null;
    switch (v.getId()) {
    case (R.id.main_activity_button_some_list):
        intent = new Intent(this, SomeListActivity.class);

        break;

    case (R.id.main_activity_button_fragment):
        intent = new Intent(this, SomeFragmentActivity.class);
        break;
    }
    startActivity(intent);
}

是我正常的MainActivity切换到它。我能够访问SomeListActivity.class,所以我不确定是什么。它在清单中的定义就好了。

编辑:

    11-09 18:48:53.652: E/AndroidRuntime(4268): FATAL EXCEPTION: main
11-09 18:48:53.652: E/AndroidRuntime(4268): java.lang.NullPointerException
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1409)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.app.Activity.startActivityForResult(Activity.java:3351)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.app.Activity.startActivityForResult(Activity.java:3312)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.app.Activity.startActivity(Activity.java:3522)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.app.Activity.startActivity(Activity.java:3490)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at com.LoLCompanionApp.MainActivity.onClick(MainActivity.java:39)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.view.View.performClick(View.java:4084)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.view.View$PerformClick.run(View.java:16966)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.os.Handler.handleCallback(Handler.java:615)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.os.Looper.loop(Looper.java:137)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at android.app.ActivityThread.main(ActivityThread.java:4931)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-09 18:48:53.652: E/AndroidRuntime(4268):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-09 18:48:53.652: E/

AndroidRuntime(4268):   at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

问题是,在调用Intent时,您的startActivity()为空。您已将点击监听器附加到ID为R.id.sidebar_button的内容,但如果ID为intentR.id.main_activity_button_some_list,则只会将R.id.main_activity_button_fragment更改为非空。

这是一个快速修复:

public void onClick(View v) {
    Intent intent = null;
    switch (v.getId()) {
    case (R.id.main_activity_button_some_list):
        intent = new Intent(this, SomeListActivity.class);

        break;

    case (R.id.main_activity_button_fragment):
        intent = new Intent(this, SomeFragmentActivity.class);
        break;
    }
    if (intent != null) // Add this
        startActivity(intent);
}

但是,我建议您为case创建新的R.id.sidebar_button