简体   繁体   中英

Activity crashing through intent launch, but not when set as main activity

So I have a FragmentActivity, but I can't switch to it. It works fine if its my apps main activity, but not when I attempt to switch to it..

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);


}

And in the onCreate of the main activity that launches that fragment:

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);


    }

Is the onCreate of it, and:

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);
}

is in my normal MainActivity to switch to it. I am able to get to the SomeListActivity.class so I'm not sure whats up. Its defined in the manifest just fine.

EDIT:

    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)

The problem is that your Intent is null upon calling startActivity() . You've attached the click listener to something with ID R.id.sidebar_button , however you only change intent to non-null if the ID is R.id.main_activity_button_some_list or R.id.main_activity_button_fragment .

Here's a quick fix:

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);
}

However, I would suggest you create a new case for R.id.sidebar_button .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM