0

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!
4

2 回答 2

0

呼……

在 Androidminifest.xml 中:

我加:

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

愚蠢的我。我以为 Elcipse 会自动完成。

于 2013-04-14T11:16:45.750 回答
0

试试这个,像这样编辑你的上下文

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

我认为你有问题是由于final Context context = this;

于 2013-04-14T11:26:39.477 回答