简体   繁体   中英

How to go to another activity from button looping?

How to go to another activity? I'm trying to go another activity from button. If I click one of the buttons, button must redirect to another actitivity, chapters.class. I tried Toast.make in OnClick function. It works. But when I coded to go another activity, apps crashed. How to solve that? Here is the code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.oldtestament);
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayoutold);
    String[] values = { "Apple","Orange","Twitter","Yahoo","Google","English","Reddit","Settings" };
    Button[] b = new Button[values.length];
    for (int i = 0; i < b.length; i++) {
        b[i] = new Button(this);
    }
    for (int i = 0; i < values.length; i++) {
        LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);             
        ((MarginLayoutParams) params).setMargins(18, 2, 18, 2);
        b[i].setText(values[i]); 
        layout.addView(b[i], params);
        addListenerOnButton(b[i]);
    }
}

public void addListenerOnButton(Button b) {
    final Context context = this;
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, chapters.class);
            startActivity(intent);
        }

    });

}

Here is LogCat:

04-14 17:24:57.168: E/AndroidRuntime(23719): FATAL EXCEPTION: main
04-14 17:24:57.168: E/AndroidRuntime(23719): android.content.ActivityNotFoundException: Unable to find explicit activity class  {com.joshclemm.android.tabswithactivity/com.joshclemm.android.tabswithactivity.chapters};  have you declared this activity in your AndroidManifest.xml?
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at and roid.app.Activity.startActivityFromChild(Activity.java:3067)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.app.Activity.startActivityForResult(Activity.java:2847)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.app.Activity.startActivity(Activity.java:2933)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at com.joshclemm.android.tabswithactivity.oldtestament$1.onClick(oldtestament.java:77)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.view.View.performClick(View.java:2552)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.view.View$PerformClick.run(View.java:9229)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.os.Handler.handleCallback(Handler.java:587)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.os.Looper.loop(Looper.java:130)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at android.app.ActivityThread.main(ActivityThread.java:3701)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at java.lang.reflect.Method.invoke(Method.java:507)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
04-14 17:24:57.168: E/AndroidRuntime(23719):    at dalvik.system.NativeStart.main(Native Method)
04-14 17:24:58.998: E/kernel(125): [70117.358337] binder: release proc 23719, transaction 2633713, not freed
04-14 17:24:58.998: E/kernel(125): [70117.358398] binder: release proc 23719, transaction 2633714, not freed
04-14 17:24:59.018: E/InputDispatcher(245): channel '2b33bae0 com.joshclemm.android.tabswithactivity/com.joshclemm.android.tabswithactivity.CustomTabActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
04-14 17:24:59.018: E/InputDispatcher(245): channel '2b33bae0 com.joshclemm.android.tabswithactivity/com.joshclemm.android.tabswithactivity.CustomTabActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

Hu....

In Androidminifest.xml:

I add:

<activity
        android:name=".chapters"
        android:label="@string/app_name" >

Stupid me. I thought Elcipse will do it automactically.

try this one, Just edit your context like this

Intent intent = new Intent(getApplicationContext(), chapters.class);
startActivity(intent);

I think you having problem due to final Context context = this;

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