片段类无法启动活动组件信息

时间:2015-04-08 09:09:35

标签: android android-fragments

我想学习片段。所以开始向tutorialspoint.com学习。 例子是:

  

此示例将向您解释如何创建自己的片段。这里   我们将创建两个片段,其中一个将在设备时使用   处于横向模式,另一个片段将用于   肖像模式。

当我运行应用程序时发生此例外:

04-08 10:57:20.135  19408-19408/net.motameni.apps.fragments D/AndroidRuntime﹕ Shutting down VM
04-08 10:57:20.137  19408-19408/net.motameni.apps.fragments E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: net.motameni.apps.fragments, PID: 19408
    java.lang.RuntimeException: Unable to start activity ComponentInfo{net.motameni.apps.fragments/net.motameni.apps.fragments.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at net.motameni.apps.fragments.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     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:454)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2123)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
            at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at net.motameni.apps.fragments.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-08 12:49:45.078  26843-26843/net.motameni.apps.fragments I/art﹕ Late-enabling -Xcheck:jni
04-08 12:49:45.445  26843-26843/net.motameni.apps.fragments D/AndroidRuntime﹕ Shutting down VM
04-08 12:49:45.452  26843-26843/net.motameni.apps.fragments E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: net.motameni.apps.fragments, PID: 26843
    java.lang.RuntimeException: Unable to start activity ComponentInfo{net.motameni.apps.fragments/net.motameni.apps.fragments.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at net.motameni.apps.fragments.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalStateException: Fragment net.motameni.apps.fragments.Raw_Fragment did not create a view.
            at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2145)
            at android.app.Activity.onCreateView(Activity.java:5282)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at net.motameni.apps.fragments.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-08 12:49:50.902  26843-26843/net.motameni.apps.fragments I/Process﹕ Sending signal. PID: 26843 SIG: 9
04-08 13:20:18.838  30365-30365/net.motameni.apps.fragments I/art﹕ Late-enabling -Xcheck:jni
04-08 13:20:19.081  30365-30384/net.motameni.apps.fragments D/OpenGLRenderer﹕ Render dirty regions requested: true
04-08 13:20:19.091  30365-30365/net.motameni.apps.fragments D/Atlas﹕ Validating map...
04-08 13:20:19.138  30365-30384/net.motameni.apps.fragments I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
04-08 13:20:19.139  30365-30384/net.motameni.apps.fragments I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-08 13:20:19.159  30365-30384/net.motameni.apps.fragments D/OpenGLRenderer﹕ Enabling debug mode 0
04-08 13:20:41.066  31240-31260/net.motameni.apps.fragments D/OpenGLRenderer﹕ Render dirty regions requested: true
04-08 13:20:41.072  31240-31240/net.motameni.apps.fragments D/Atlas﹕ Validating map...
04-08 13:20:41.114  31240-31260/net.motameni.apps.fragments I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
04-08 13:20:41.115  31240-31260/net.motameni.apps.fragments I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-08 13:20:41.135  31240-31260/net.motameni.apps.fragments D/OpenGLRenderer﹕ Enabling debug mode 0
04-08 13:21:02.984  32157-32207/net.motameni.apps.fragments D/OpenGLRenderer﹕ Render dirty regions requested: true
04-08 13:21:02.992  32157-32157/net.motameni.apps.fragments D/Atlas﹕ Validating map...
04-08 13:21:03.031  32157-32207/net.motameni.apps.fragments I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
04-08 13:21:03.032  32157-32207/net.motameni.apps.fragments I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-08 13:21:03.053  32157-32207/net.motameni.apps.fragments D/OpenGLRenderer﹕ Enabling debug mode 0

为什么????? 我认为这可能与API级别有关(我的应用程序API级别为21)。 请参阅示例链接和帮助。感谢。

MainActivity.java

package net.motameni.apps.fragments;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Configuration config = getResources().getConfiguration();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        /**
         * Check the device orientation and act accordingly
         */
        if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
            //Landscape mode of the device
            LM_Fragment lm_fragment = new LM_Fragment();
            fragmentTransaction.replace(android.R.id.content,lm_fragment);
        }else{
            //Portrait mode of the device
            PM_Fragment pm_fragment = new PM_Fragment();
            fragmentTransaction.replace(android.R.id.content,pm_fragment);
        }
        fragmentTransaction.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:baselineAligned="false"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <fragment
        android:name="net.motameni.apps.fragments"
        android:id="@+id/lm_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

    <fragment
        android:name="net.motameni.apps.fragments"
        android:id="@+id/pm_fragment"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

</LinearLayout>

lm_fragment.xml (与pm_fragment.xml相同):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="landscape"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="20sp" />
</LinearLayout>

LM_Fragment.java (与RM_Fragment相同):

package net.motameni.apps.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LM_Fragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /**
         * Inflate the layout for this fragment
         */
        return inflater.inflate(R.layout.lm_fragment,container,false);
    }
}

3 个答案:

答案 0 :(得分:0)

首先,namefragment的{​​{1}}应该是您activity_main.xml的绝对路径。在这种情况下,fragment应为{{1} }} 其次,在name行中,net.motameni.apps.fragments.LM_Fragment应为fragmentTransaction.replace(android.R.id.content,pm_fragment);,而不是R。前者是您生成的your-pkg-name.R,后者是android.R生成的。 第三,如果您想添加R,则需要将Android SDK更改为:

pm_fragment

与此同时,您需要更改代码:

activity_main.xml

最重要的是,<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:baselineAligned="false" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <FrameLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> fragmentTransaction.replace(R.id.content,pm_fragment);

答案 1 :(得分:0)

在您的片段XML中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

您必须向其添加上下文并将其更改为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.blaw.blawblaw">

然后在您的主XMl中:

<fragment
        android:name="net.motameni.apps.fragments"
        android:id="@+id/lm_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

您没有这个片段"net.motameni.apps.fragments"!您应该将其更改为"com.blaw.blawblaw",然后该片段将显示在您的页面上!

并且您只需要XML中的一个片段,添加两个片段,那是错误的 只需添加一个,然后在您的主类上处理片段更改即可!

最好的问候!

答案 2 :(得分:-1)

xml中的标记fragment应为:Fragment,它使用 大写字母。