当onStart(onRestart)在onCreate之后时,活动崩溃

时间:2014-03-20 06:28:01

标签: java android android-activity onstart

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

发生了一个奇怪的错误:当我在java活动类的onCreate之后使用onStart方法时,活动会立即崩溃。没有onStart(只有onCreate),一切正常。同样的事情是当我把onRestart放在onStart上时。

它发生在我的两个不同的活动,主要和帮助,这是不相似的。重要的是我需要使用onStart(onRestart):

    主要活动中的
  • 我希望每次出现时,在第一次加载时以及从其他活动返回后都淡化布局,

  • HelpActivity使用相同框架(ScrollView)中的帮助页面来扩充其他子视图,但在设备轮换时,它会返回到帮助TOC,最初设置为onCreate中的框架。你知道,这不是用户所希望的。

应用代码和Logcat。

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

如果有人遇到同样的问题,请告诉我这是一个Android错误还是我做错了什么。也许我可以过度工作,但根据文档blablabla的传统方式,将是更好和更方便。

提前Thanx!

MainActivity:

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

HelpActivity:

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

1 个答案:

答案 0 :(得分:0)

您的layoutHelpFrame不能同时是scrollview和linearLayout。你必须混淆那些ids或类似的东西。

为scrollview和linearLayout提供不同的ID并正确调用它,它应该可以正常工作。