gpt4 book ai didi

java - 单击 Android 应用程序中的按钮时导航失败,错误为 "Unfortunately, ' myapp' 已停止”

转载 作者:行者123 更新时间:2023-11-30 11:04:56 24 4
gpt4 key购买 nike

注意:我看过其他解决方案,但没有一个对我有帮助。

我的应用程序有一个 mainactivity,它有一个按钮可以导航到第二个屏幕,该屏幕有一个随机移动的按钮。当按钮点击次数超过计数时,它应该导航到第三个屏幕。我可以单击第二个屏幕中的按钮,直到计数刚好超过为止。但它没有导航到第三个屏幕,我收到堆栈跟踪错误。

我在下面发布我的代码:

MainActivity -> FirstActivity -> 结果

主要 Activity .java:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
Button btn = (Button)findViewById(R.id.firstActivity);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
startActivity(intent);
}
});
}

FirstActivity.java:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);

//Button - Random Placement on the screen
showPopUp = (ImageButton) findViewById(R.id.showPopUp);
absParams = (AbsoluteLayout.LayoutParams) showPopUp.getLayoutParams();
displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
width = displaymetrics.widthPixels;
height = displaymetrics.heightPixels;
r = new Random();
absParams.x = r.nextInt(width - 50);
absParams.y = r.nextInt(height - 50);
showPopUp.setLayoutParams(absParams);

//OnClickListener
showPopUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (clickCount >= Count-1) {
Intent result = new Intent(FirstActivity.this, ShowResult.class);
startActivity(result);
}
else {
clickCount = clickCount + 1;
r = new Random();
absParams.x = r.nextInt(width - 50);
absParams.y = r.nextInt(height - 50);
showPopUp.setLayoutParams(absParams);
}
}
});
}

最后是我的 Result.java:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);

setResult(RESULT_OK);
finish();
}

日志如下:

WindowManagerService.removeWindowInnerLocked:2744 com.android.server.wm.WindowManagerService.removeWindowLocked:2640 com.android.server.wm.WindowManagerService.removeWindow:2562 04-24 12:54:27.411 2934-2934/? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.miotasolutions.t2t, PID: 2934 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.miotasolutions.t2t/com.miotasolutions.t2t.util.ShowResult}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485) at android.app.Activity.startActivityForResult(Activity.java:3736) at android.app.Activity.startActivityForResult(Activity.java:3697) at android.app.Activity.startActivity(Activity.java:4007) at android.app.Activity.startActivity(Activity.java:3975) at com.miotasolutions.t2t.util.FirstActivity$1.onClick(FirstActivity.java:51) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 04-24 12:54:32.037 1261-1328/system_process E/InputDispatcher﹕ channel '3a1187fd com.miotasolutions.t2t/com.miotasolutions.t2t.FullscreenActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

下面是我的 AndroidManifest.xml 文件:

<activity
android:name=".FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".util.FirstActivity"
android:label="@string/title_activity_first" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".util.ShowResult"
android:label="@string/title_activity_result" >
</activity>

最佳答案

查看 Result Activity 的代码,如果您在 onCreate 方法上调用 finish(),该 Activity 将在创建后立即关闭,因此不会发生任何可见的事情。

尝试注释掉 finish(),或者在此处放置一个断点以查看它是否正常工作。

关于java - 单击 Android 应用程序中的按钮时导航失败,错误为 "Unfortunately, ' myapp' 已停止”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29840362/

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