Android应用程序在Inflater.inflate上崩溃

时间:2014-02-15 07:27:08

标签: java android xml android-inflate

好的,所以这就是事情,我看了很多视频并阅读了大量的文章,但我找不到解决方案......

基本上,我的应用程序崩溃了,因为我正在膨胀,我无法找到解决方案。

JAVA:

package com.iweto;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class TestScreen extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        MyFragment frag = new MyFragment();
        transaction.add(R.id.fragment_container, frag);
        transaction.commit();
        setContentView(R.layout.test_screen);
    }
    @Override
    protected void onStart() {
        super.onStart();

    }
    public static class MyFragment extends Fragment {

        private View view;
        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            return inflater.inflate(R.layout.main, container, false);
        } catch (InflateException e) {
            /* map is already there, just return view as it is */
            e.getStackTrace();
        }
        return view;
    }       
}   
}

XMLS:

test_screen.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"
 >
<LinearLayout
android:orientation="vertical"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>

</LinearLayout>

最后是main.xml文件:

 <?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/main" >
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 >
 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:width="350sp"
     android:height="350sp"
        android:layout_gravity="center"
     android:text="testestestest"
     ></Button>
 </LinearLayout>
 </fragment>

更多的是,这是logcat:

02-15 09:45:59.566: D/AndroidRuntime(1086): Shutting down VM
02-15 09:45:59.566: W/dalvikvm(1086): threadid=1: thread exiting with uncaught exception         (group=0xa628d288)
02-15 09:45:59.570: E/AndroidRuntime(1086): FATAL EXCEPTION: main
02-15 09:45:59.570: E/AndroidRuntime(1086): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.iweto/com.iweto.TestScreen}: android.view.InflateException:     Binary XML file line #2: Error inflating class fragment
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.ActivityThread.access$600(ActivityThread.java:130)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.os.Handler.dispatchMessage(Handler.java:99)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.os.Looper.loop(Looper.java:137)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.ActivityThread.main(ActivityThread.java:4745)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     java.lang.reflect.Method.invokeNative(Native Method)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     java.lang.reflect.Method.invoke(Method.java:511)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     dalvik.system.NativeStart.main(Native Method)
02-15 09:45:59.570: E/AndroidRuntime(1086): Caused by: android.view.InflateException:     Binary XML file line #2: Error inflating class fragment
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at      android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     com.iweto.TestScreen$MyFragment.onCreateView(TestScreen.java:44)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.BackStackRecord.run(BackStackRecord.java:635)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.Activity.performStart(Activity.java:5017)
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
02-15 09:45:59.570: E/AndroidRuntime(1086):     ... 11 more
02-15 09:45:59.570: E/AndroidRuntime(1086): Caused by: java.lang.NullPointerException:     name == null
02-15 09:45:59.570: E/AndroidRuntime(1086):     at     java.lang.VMClassLoader.findLoadedClass(Native Method)
 02-15 09:45:59.570: E/AndroidRuntime(1086):    at     java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
 02-15 09:45:59.570: E/AndroidRuntime(1086):    at     java.lang.ClassLoader.loadClass(ClassLoader.java:491)
 02-15 09:45:59.570: E/AndroidRuntime(1086):    at     java.lang.ClassLoader.loadClass(ClassLoader.java:461)
 02-15 09:45:59.570: E/AndroidRuntime(1086):    at     android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:436)
 02-15 09:45:59.570: E/AndroidRuntime(1086):    at     android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:255)
 02-15 09:45:59.570: E/AndroidRuntime(1086):    at     android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-15 09:45:59.570: E/AndroidRuntime(1086):     ... 20 more

2 个答案:

答案 0 :(得分:2)

setContentView

之后添加您的片段代码
  

的setContentView(R.layout.test_screen);

setContentView初始化您的test_screen,之后您才能访问其内容fragment container

答案 1 :(得分:0)

移动setContentView(R.layout.test_screen); 在行super.onCreate(savedInstanceState);

之后