我有一个按钮,当点击它时我想加载一个新视图。
我正在使用这个主要活动类
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonPressed(View v){
Intent i = new Intent(this, SwitchedActivity.class);
startActivity(i);
}
}
然后我想加载其他活动
public class SwitchedActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
}
}
那么应该发生什么,是我单击activity_main.xml中的按钮,然后从activity_two.xml加载布局。
问题是,当我点击按钮时,应用程序就崩溃了。
我得到了这个logcat堆栈跟踪:
08-18 11:36:09.078: E/AndroidRuntime(1232): FATAL EXCEPTION: main
08-18 11:36:09.078: E/AndroidRuntime(1232): Process: com.johncorser.myapp, PID: 1232
08-18 11:36:09.078: E/AndroidRuntime(1232): java.lang.IllegalStateException: Could not execute method of the activity
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.view.View$1.onClick(View.java:3823)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.view.View.performClick(View.java:4438)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.view.View$PerformClick.run(View.java:18422)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.os.Handler.handleCallback(Handler.java:733)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.os.Handler.dispatchMessage(Handler.java:95)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.os.Looper.loop(Looper.java:136)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-18 11:36:09.078: E/AndroidRuntime(1232): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 11:36:09.078: E/AndroidRuntime(1232): at java.lang.reflect.Method.invoke(Method.java:515)
08-18 11:36:09.078: E/AndroidRuntime(1232): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-18 11:36:09.078: E/AndroidRuntime(1232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-18 11:36:09.078: E/AndroidRuntime(1232): at dalvik.system.NativeStart.main(Native Method)
08-18 11:36:09.078: E/AndroidRuntime(1232): Caused by: java.lang.reflect.InvocationTargetException
08-18 11:36:09.078: E/AndroidRuntime(1232): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 11:36:09.078: E/AndroidRuntime(1232): at java.lang.reflect.Method.invoke(Method.java:515)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.view.View$1.onClick(View.java:3818)
08-18 11:36:09.078: E/AndroidRuntime(1232): ... 11 more
08-18 11:36:09.078: E/AndroidRuntime(1232): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johncorser.myapp/com.johncorser.myapp.SwitchedActivity}; have you declared this activity in your AndroidManifest.xml?
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.app.Activity.startActivityForResult(Activity.java:3424)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.app.Activity.startActivityForResult(Activity.java:3385)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.app.Activity.startActivity(Activity.java:3627)
08-18 11:36:09.078: E/AndroidRuntime(1232): at android.app.Activity.startActivity(Activity.java:3595)
08-18 11:36:09.078: E/AndroidRuntime(1232): at com.johncorser.myapp.MainActivity.buttonPressed(MainActivity.java:40)
08-18 11:36:09.078: E/AndroidRuntime(1232): ... 14 more
公平警告,我可能做了一些非常愚蠢的事情,我是新人。
答案 0 :(得分:2)
答案在于你的logcat:
08-18 11:36:09.078:E / AndroidRuntime(1232):引起:android.content.ActivityNotFoundException:无法找到显式活动类{com.johncorser.myapp / com.johncorser.myapp.SwitchedActivity};你有没有在AndroidManifest.xml中声明这个活动?
所以将它添加到您的清单中就像这样
<activity
android:name="SwitchedActivity"
android:label="Switched">
</activity>
每当您添加新活动时,请务必将其添加到您的清单。
答案 1 :(得分:1)
我非常确定您没有在AndroidManifest.xml
文件中声明第二项活动。
你应该在你的清单中:
<application ... >
<activity
android:name=".MainActivity"
... >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SwitchedActivity"
... >
</activity>
...
</application>
答案 2 :(得分:0)
将第二个活动添加到清单文件中。您发布的日志清楚地说明了问题。
将以下内容添加到您的清单中:
<activity
android:name=".SwitchedActivity"/>
请确保您下次阅读logcats或google logcat消息。