Android:应用程序由于未知原因而崩溃

Posted

技术标签:

【中文标题】Android:应用程序由于未知原因而崩溃【英文标题】:Android: App crashes due to unknown reasons 【发布时间】:2017-04-22 17:54:35 【问题描述】:

我有一个 android 应用,在这个 Activity 中有两个片段,我没有在片段中添加任何代码,这里是 Activity:

schedule_settings.java

public class schedule_settings extends AppCompatActivity 

    Fragment fragment;
    Button dailyButton, weeklyButton;

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

        setContentView(R.layout.activity_schedule_settings);      //The error occurs here according to log cat...

        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);

        if (getActionBar() != null) 
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setTitle("Schedule Settings");
            getSupportActionBar().hide();
         else if (getSupportActionBar() != null) 
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Schedule Settings");
        

        dailyButton = (Button) findViewById(R.id.scheduleSettingsDailyButton);
        weeklyButton = (Button) findViewById(R.id.scheduleSettingsWeeklyButton);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        //getLoaderManager().initLoader(0, null, );
    

    public void launchWeeklyFragment(View view) 
        fragment = new schedule_settings_weekly();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.scheduleSettingsFragment, fragment);
        fragmentTransaction.commit();

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

        dailyButton.setBackground(getResources().getDrawable(R.drawable.left_rounded_button_not_clicked));
        weeklyButton.setBackground(getResources().getDrawable(R.drawable.right_rounded_button_clicked));
    

    public void launchDailyFragment(View view) 
        fragment = new schedule_setting_daily();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.scheduleSettingsFragment, fragment);
        fragmentTransaction.commit();

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

        dailyButton.setBackground(getResources().getDrawable(R.drawable.left_rounded_button_clicked));
        weeklyButton.setBackground(getResources().getDrawable(R.drawable.right_rounded_button_not_clicked));
    

这是相同的xml,它由activity_schedule_settings.xmlcontent_schedule_settings.xml 两部分组成。这是activity_schedule_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.android.smartswitch.schedule_settings">

    <android.support.design.widget.AppBarLayout
        android:layout_
        android:layout_
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_schedule_settings" />

</android.support.design.widget.CoordinatorLayout>

这里是 content_schedule_settings_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.android.smartswitch.schedule_settings"
    tools:showIn="@layout/activity_schedule_settings">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">


        <LinearLayout
            android:layout_
            android:layout_
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="20dp">

            <Button
                android:id="@+id/scheduleSettingsDailyButton"
                android:layout_
                android:layout_
                android:background="@drawable/left_rounded_button_clicked"
                android:onClick="launchDailyFragment"
                android:text="Daily" />

            <Button
                android:id="@+id/scheduleSettingsWeeklyButton"
                android:layout_
                android:layout_
                android:background="@drawable/right_rounded_button_not_clicked"
                android:onClick="launchWeeklyFragment"
                android:text="Weekly" />

        </LinearLayout>

        <fragment
            android:id="@+id/scheduleSettingsFragment"
            android:layout_
            android:layout_ />

    </LinearLayout>

</ScrollView>

错误发生在marked java 部分。这是日志猫跟踪,我不知道该怎么做,因为它将我实际将 xml 文件分配给 java 的部分标记为错误:

04-22 16:55:51.045 12130-12130/com.example.android.smartswitch E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.android.smartswitch, PID: 12130
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.android.smartswitch/com.example.android.smartswitch.schedule_settings: android.view.InflateException: Binary XML file line #43: Binary XML file line #43: Error inflating class fragment
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                              Caused by: android.view.InflateException: Binary XML file line #43: Binary XML file line #43: Error inflating class fragment
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
                                                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
                                                                                 at com.example.android.smartswitch.schedule_settings.onCreate(schedule_settings.java:25)
                                                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                              Caused by: android.view.InflateException: Binary XML file line #43: Error inflating class fragment
                                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                                                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
                                                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                                                                                 at com.example.android.smartswitch.schedule_settings.onCreate(schedule_settings.java:25) 
                                                                                 at android.app.Activity.performCreate(Activity.java:6237) 
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                              Caused by: java.lang.NullPointerException
                                                                                 at java.lang.VMClassLoader.findLoadedClass(Native Method)
                                                                                 at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)
                                                                                 at java.lang.ClassLoader.loadClass(ClassLoader.java:499)
                                                                                 at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                                 at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:459)
                                                                                 at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3321)
                                                                                 at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                                 at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
                                                                                 at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
                                                                                 at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
                                                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
                                                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                                                                                 at com.example.android.smartswitch.schedule_settings.onCreate(schedule_settings.java:25) 
                                                                                 at android.app.Activity.performCreate(Activity.java:6237) 
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我是 android 新手,这个错误给我带来了很多问题,我试图用谷歌搜索,但找不到任何相关信息。请帮忙...

【问题讨论】:

【参考方案1】:

java.lang.RuntimeException:无法启动活动 组件信息com.example.android.smartswitch/com.example.android.smartswitch.schedule_settings: android.view.InflateException:二进制 XML 文件第 43 行:二进制 XML 文件line #43:膨胀class片段时出错

您应该在content_schedule_settings XML 中将class 名称添加到您的&lt;fragment /&gt;

试试这个:

<fragment
    android:id="@+id/scheduleSettingsFragment"
    android:layout_
    android:layout_
    class="com.example.android.smartswitch.schedule_settings_weekly" />

希望这会完美运行。

【讨论】:

以上是关于Android:应用程序由于未知原因而崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android 应用程序不断因空指针异常而崩溃

iOS 应用程序因未知原因崩溃

线程:PyQt 因“出队时队列中的未知请求”而崩溃

iOS 崩溃:由于未捕获的异常原因而终止应用程序:UIPopoverPresentationController 应该有一个非零的 sourceView

Play 项目中的简单 SBT 模块导致 Play 因未知原因而崩溃 - play 2.1

如何防止android应用程序由于后台线程异常而崩溃?