2017-04-22 153 views
0

我有一個Android應用程序,在這個活動有兩個片段,我還沒有添加任何代碼片段,這裏的活動:安卓應用程序崩潰,由於未知的原因

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_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.example.android.smartswitch.schedule_settings"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      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_width="match_parent" 
    android:layout_height="match_parent" 
    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_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:gravity="center" 
      android:orientation="horizontal" 
      android:padding="20dp"> 

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

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

     </LinearLayout> 

     <fragment 
      android:id="@+id/scheduleSettingsFragment" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" /> 

    </LinearLayout> 

</ScrollView> 

誤差在標記 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 ComponentInfo{com.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和這個錯誤給我很多問題,我試圖谷歌它,但無法找到任何相關的。請幫助...

回答

0

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.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

您應該class名稱添加到您<fragment />content_schedule_settings XML。

試試這個:

<fragment 
    android:id="@+id/scheduleSettingsFragment" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    class="com.example.android.smartswitch.schedule_settings_weekly" /> 

希望這將很好地工作。