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

发生一个奇怪的错误:当我在java活动类中的onCreate之后使用onStart方法时,活动在启动时立即崩溃。而没有onStart的话一切正常(仅使用onCreate)。同样的事情是当我放置onRestart而不是onStart时。

它发生在我的两个不同的活动中,即Main和Help,这两个活动并不相似。重要为什么我需要使用onStart(onRestart):


在MainActivity中,我希望在每次出现时,在第一次加载以及从其他活动返回后淡入布局,
HelpActivity使用同一框架(ScrollView)中的帮助页面来扩展其他子视图,但是在设备旋转时,它会返回到“帮助目录”,最初是在onCreate中设置为该框架。您会看到,这不是用户想要的。


将应用代码和Logcat。

我将旧的电子阅读器Pocketbook iq 701与Android 2.0.1(通过USB插入)和第三方仿真器YouVawe一起使用,并且两者均出现错误。

如果有人遇到过同样的问题,请告诉我这是Android错误还是我做错了什么。也许我可能会做得过多,但是根据blablabla博士的常规方法,它会更可取,更方便。

提前感谢!

主要活动:

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);
    }


帮助活动:

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不能同时是滚动视图和linearLayout。您必须混淆ID或类似的东西。

给滚动视图和linearLayout指定一个不同的ID,并正确调用它们,它应该可以工作。

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

10-10 18:26