gpt4 book ai didi

java - startActivity 使应用程序强制关闭

转载 作者:行者123 更新时间:2023-12-01 07:14:22 24 4
gpt4 key购买 nike

我刚刚开始 Android 开发,我被这段代码困住了:

public class Home extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button buttonFoo = (Button)findViewById(R.id.buttonFoo);
buttonFoo.setOnClickListener(openFoo);
}

public OnClickListener openFoo = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Home.this, Foo.class);
startActivity(intent);
}
};
}

这是 Activity Foo 的代码:

public class Foo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//populateWithLatest();
}
}

以及错误日志:

07-18 19:53:35.043: ERROR/AndroidRuntime(4656): FATAL EXCEPTION: main
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.github.progval.openquote/com.github.progval.openquote.sites.FooActivity}; have you declared this activity in your AndroidManifest.xml?
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.Activity.startActivityForResult(Activity.java:2827)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.Activity.startActivity(Activity.java:2933)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at com.github.progval.openquote.Home$1.onClick(Home.java:34)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.view.View.performClick(View.java:2501)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.view.View$PerformClick.run(View.java:9107)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.os.Handler.handleCallback(Handler.java:587)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.os.Looper.loop(Looper.java:130)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.ActivityThread.main(ActivityThread.java:3835)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at dalvik.system.NativeStart.main(Native Method)

除非我注释了 startActivity(intent); 行,否则当我按下 Foo 按钮时应用程序会强制关闭。

最佳答案

<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name=".Home"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".Foo"></activity>

这是我最喜欢的部分:

07-18 19:53:35.043: 错误/AndroidRuntime(4656): android.content.ActivityNotFoundException: 无法找到显式 Activity 类 {com.github.progval.openquote/com.github.progval.openquote.sites。 Vdm Activity }; 您是否在 AndroidManifest.xml 中声明了此 Activity ?

我不知道这是否是实现按钮的最佳方式,但我是这样做的:

findViewById(R.id.buttonFoo).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(v.getContext(), Foo.class);
startActivity(i);
}
});

关于java - startActivity 使应用程序强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com