0

所以我有一个 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)
4

1 回答 1

0

问题是您Intent在调用时为空startActivity()。您已将点击侦听器附加到具有 ID的内容,但是如果 ID 为或R.id.sidebar_button,则仅更改intent为非 null 。R.id.main_activity_button_some_listR.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);
}

但是,我建议您caseR.id.sidebar_button.

于 2012-11-10T00:23:28.147 回答