膨胀失败

时间:2014-02-08 12:56:41

标签: java android xml layout-inflater

我运行了一个关于片段的基本代码。 我可以完美地运行代码,但是当我在MainActivity.java中插入一个简单的if-else时。它无法膨胀。任何的想法? 这是Activity_main.xml

<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<fragment
    android:name = "com.example.frags.LF"
    android:id="@+id/viewer"
    android:layout_height="0dp"
    android:layout_width="match_parent"/>
<fragment
android:name="com.example.frags.MF"
android:id="@+id/land2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RelativeLayout>

这是MainActivity.java

package com.example.frags;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private static final String TAG = "MainAct";
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG,"Entered on Create()");
    super.onCreate(savedInstanceState);
    Log.i(TAG,"Entered on CreatedUPRT()");
    setContentView(R.layout.activity_main);
    Configuration config = getResources().getConfiguration();
    if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
        Log.i(TAG,"Entered portrait mode");
        LF mt = (LF) getFragmentManager()
                .findFragmentById(R.id.viewer);
    }
    else
    {
        MF mt = (MF)getFragmentManager().findFragmentById(R.id.land2);
    }       
}

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

}

LF.java     package com.example.frags;

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

public class LF extends Fragment {
private static final String TAG = "Fragment";
@Override
public View onCreateView(LayoutInflater l, ViewGroup V, Bundle savedInstanceState)
{
    Log.i(TAG,"Entered onCreateView");
    return l.inflate(R.layout.lf, V, false);
    }

}

MF.java是

package com.example.frags;

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

public class MF extends Fragment {

    @Override
    public View onCreateView(LayoutInflater m, ViewGroup V, Bundle savedInstanceState){
        return m.inflate(R.id.land2, V, false);

    }
}

和mf.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/land" />

</LinearLayout>

lf.xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text" />

</LinearLayout>

Logcat输出

02-08 18:11:09.285: I/ActivityThread(3757): Switching default density from 320 to 300
02-08 18:11:09.305: I/MainAct(3757): Entered on Create()
02-08 18:11:09.305: I/MainAct(3757): Entered on CreatedUPRT()
02-08 18:11:09.415: I/Fragment(3757): Entered onCreateView
02-08 18:11:09.425: E/ActivityThread(3757): Failed to inflate
02-08 18:11:09.425: E/ActivityThread(3757): android.view.InflateException: Binary XML file line #16: Error inflating class fragment
02-08 18:11:09.425: E/ActivityThread(3757):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-08 18:11:09.425: E/ActivityThread(3757):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:354)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.Activity.setContentView(Activity.java:1953)
02-08 18:11:09.425: E/ActivityThread(3757):     at com.example.frags.MainActivity.onCreate(MainActivity.java:17)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.Activity.performCreate(Activity.java:5480)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.ActivityThread.access$800(ActivityThread.java:145)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.os.Looper.loop(Looper.java:136)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.ActivityThread.main(ActivityThread.java:5088)
02-08 18:11:09.425: E/ActivityThread(3757):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 18:11:09.425: E/ActivityThread(3757):     at java.lang.reflect.Method.invoke(Method.java:515)
02-08 18:11:09.425: E/ActivityThread(3757):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:783)
02-08 18:11:09.425: E/ActivityThread(3757):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-08 18:11:09.425: E/ActivityThread(3757):     at dalvik.system.NativeStart.main(Native Method)
02-08 18:11:09.425: E/ActivityThread(3757): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
02-08 18:11:09.425: E/ActivityThread(3757):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2344)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.content.res.Resources.getLayout(Resources.java:939)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
02-08 18:11:09.425: E/ActivityThread(3757):     at com.example.frags.MF.onCreateView(MF.java:13)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.Fragment.performCreateView(Fragment.java:1700)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.app.Activity.onCreateView(Activity.java:4867)
02-08 18:11:09.425: E/ActivityThread(3757):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
02-08 18:11:09.425: E/ActivityThread(3757):     ... 21 more
02-08 18:11:09.435: D/AndroidRuntime(3757): Shutting down VM
02-08 18:11:09.435: W/dalvikvm(3757): threadid=1: thread exiting with uncaught exception (group=0x4182dc80)
02-08 18:11:09.445: E/AndroidRuntime(3757): FATAL EXCEPTION: main
02-08 18:11:09.445: E/AndroidRuntime(3757): Process: com.example.frags, PID: 3757
02-08 18:11:09.445: E/AndroidRuntime(3757): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.frags/com.example.frags.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.ActivityThread.access$800(ActivityThread.java:145)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.os.Looper.loop(Looper.java:136)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.ActivityThread.main(ActivityThread.java:5088)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at java.lang.reflect.Method.invoke(Method.java:515)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:783)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at dalvik.system.NativeStart.main(Native Method)
02-08 18:11:09.445: E/AndroidRuntime(3757): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:354)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.Activity.setContentView(Activity.java:1953)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at com.example.frags.MainActivity.onCreate(MainActivity.java:17)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.Activity.performCreate(Activity.java:5480)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
02-08 18:11:09.445: E/AndroidRuntime(3757):     ... 11 more
02-08 18:11:09.445: E/AndroidRuntime(3757): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2344)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.content.res.Resources.getLayout(Resources.java:939)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at com.example.frags.MF.onCreateView(MF.java:13)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.Fragment.performCreateView(Fragment.java:1700)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.app.Activity.onCreateView(Activity.java:4867)
02-08 18:11:09.445: E/AndroidRuntime(3757):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
02-08 18:11:09.445: E/AndroidRuntime(3757):     ... 21 more
02-08 18:11:09.455: I/Process(3757): Sending signal. PID: 3757 SIG: 9

1 个答案:

答案 0 :(得分:3)

因为它没有在第13行找到资源ID#0x7f080001