gpt4 book ai didi

java - 当 onStart (onRestart) 在 onCreate 之后执行时 Activity 崩溃

转载 作者:行者123 更新时间:2023-12-02 06:02:59 25 4
gpt4 key购买 nike

我正在开发我的第一个 Android 应用程序(拥有在其他系统(例如 Visual Basic)中进行开发的经验)。

发生一个奇怪的错误:当我在java Activity 类中的onCreate之后使用onStart方法时, Activity 在启动时立即崩溃。然而,如果没有 onStart(仅使用 onCreate),一切都可以正常工作。当我使用 onRestart 而不是 onStart 时,也会出现同样的情况。

它发生在我的两个不同的 Activity 中,即“主要”和“帮助”,这两个 Activity 并不相似。为什么我需要使用 onStart (onRestart) 很重要:

  • 在 MainActivity 中,我希望在每次出现时、第一次加载时以及从其他 Activity 返回后淡入布局,

  • HelpActivity 会在同一框架 (ScrollView) 中使用帮助页面来扩展其他 subview ,但在设备旋转时,它会返回到最初设置为 onCreate 中的框架的帮助目录。你看,这不是用户想要的。

应用代码和 Logcat。

我使用带有 Android 2.0.1(通过 USB 插入)的旧电子阅读器 Pocketbook iq 701 和第三方模拟器 YouVawe,两者均出现错误。

如果有人遇到同样的问题,请告诉我这是 Android 错误还是我做错了什么。也许我可能会过度劳累,但根据 blablabla 文档,传统的方式会更好、更方便。

提前致谢!

主要 Activity :

public class MainActivity extends Activity {

public Animation fadeIn;
public View mainView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
mainView=(View)findViewById(R.id.linearLayoutMain);
mainView.setVisibility(View.INVISIBLE); //to fadein later

//fade-in animation to use on activity (re)start
fadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
}

@Override
protected void onStart() {
super.onStart();

mainView.setVisibility(View.INVISIBLE);
mainView.startAnimation(fadeIn);
mainView.setVisibility(View.VISIBLE);
}

帮助 Activity :

public class HelpActivity extends Activity {

public LinearLayout layoutHelpFrame;
public View viewHelpPage;
public LayoutInflater inflater;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_help);

//frame to display help content
layoutHelpFrame = (LinearLayout)findViewById(R.id.frameHelp);

//load initially the Help TOC
inflater = getLayoutInflater();
viewHelpPage=inflater.inflate(R.layout.help_0, null);
//if no onStart or if onRestart used, add view here:
// layoutHelpFrame.addView(viewHelpPage);
}

@Override
protected void onStart() {
super.onStart();
//when restarted on orientation change, help page is restored
layoutHelpFrame.addView(viewHelpPage);
}

//on click topic links
public void showHelpTopic(View topiclnk) {

//clear view
layoutHelpFrame.removeView(viewHelpPage);

//get corresponding topic Id by link textview Id
String lnkname=getResources().getResourceEntryName(topiclnk.getId());
String topicN=lnkname.substring(lnkname.length()-1); //1 last char (digit)
int layoutID = MainActivity.getResId("help_"+topicN, R.layout.class);

//load the Help topic into frame
viewHelpPage=inflater.inflate(layoutID, null);
layoutHelpFrame.addView(viewHelpPage);
layoutHelpFrame.scrollTo(0, 0);
}

LogCat(自崩溃那一刻起):

03-20 06:19:18.136: D/AndroidRuntime(10243): Shutting down VM
03-20 06:19:18.136: W/dalvikvm(10243): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
03-20 06:19:18.136: E/AndroidRuntime(10243): Uncaught handler: thread main exiting due to uncaught exception
03-20 06:19:18.146: E/AndroidRuntime(10243): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.a_z_b_u_k_a.ourabc/ru.a_z_b_u_k_a.ourabc.HelpActivity}: java.lang.ClassCastException: android.widget.ScrollView
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2484)
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1835)
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.os.Looper.loop(Looper.java:123)
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.app.ActivityThread.main(ActivityThread.java:4325)
03-20 06:19:18.146: E/AndroidRuntime(10243): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 06:19:18.146: E/AndroidRuntime(10243): at java.lang.reflect.Method.invoke(Method.java:521)
03-20 06:19:18.146: E/AndroidRuntime(10243): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-20 06:19:18.146: E/AndroidRuntime(10243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-20 06:19:18.146: E/AndroidRuntime(10243): at dalvik.system.NativeStart.main(Native Method)
03-20 06:19:18.146: E/AndroidRuntime(10243): Caused by: java.lang.ClassCastException: android.widget.ScrollView
03-20 06:19:18.146: E/AndroidRuntime(10243): at ru.a_z_b_u_k_a.ourabc.HelpActivity.onCreate(HelpActivity.java:25)
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-20 06:19:18.146: E/AndroidRuntime(10243): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2431)
03-20 06:19:18.146: E/AndroidRuntime(10243): ... 11 more
03-20 06:19:18.156: I/dalvikvm(10243): threadid=7: reacting to signal 3
03-20 06:19:18.156: E/dalvikvm(10243): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
03-20 06:19:57.116: D/AndroidRuntime(10322): Shutting down VM
03-20 06:19:57.116: W/dalvikvm(10322): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
03-20 06:19:57.116: E/AndroidRuntime(10322): Uncaught handler: thread main exiting due to uncaught exception
03-20 06:19:57.126: E/AndroidRuntime(10322): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.a_z_b_u_k_a.ourabc/ru.a_z_b_u_k_a.ourabc.MainActivity}: java.lang.NullPointerException
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2484)
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1835)
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.os.Looper.loop(Looper.java:123)
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.app.ActivityThread.main(ActivityThread.java:4325)
03-20 06:19:57.126: E/AndroidRuntime(10322): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 06:19:57.126: E/AndroidRuntime(10322): at java.lang.reflect.Method.invoke(Method.java:521)
03-20 06:19:57.126: E/AndroidRuntime(10322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-20 06:19:57.126: E/AndroidRuntime(10322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-20 06:19:57.126: E/AndroidRuntime(10322): at dalvik.system.NativeStart.main(Native Method)
03-20 06:19:57.126: E/AndroidRuntime(10322): Caused by: java.lang.NullPointerException
03-20 06:19:57.126: E/AndroidRuntime(10322): at ru.a_z_b_u_k_a.ourabc.MainActivity.onCreate(MainActivity.java:44)
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-20 06:19:57.126: E/AndroidRuntime(10322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2431)
03-20 06:19:57.126: E/AndroidRuntime(10322): ... 11 more
03-20 06:19:57.136: I/dalvikvm(10322): threadid=7: reacting to signal 3
03-20 06:19:57.136: E/dalvikvm(10322): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

最佳答案

您的layoutHelpFrame不能同时是 ScrollView 和线性布局。你肯定混淆了 id 或类似的东西。

给 ScrollView 和线性布局一个不同的id并正确调用它们,它应该可以工作。

关于java - 当 onStart (onRestart) 在 onCreate 之后执行时 Activity 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525074/

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