【问题标题】:android.view.InfalteException. Why?android.view.InflateException。为什么?
【发布时间】:2013-01-30 06:13:37
【问题描述】:

我是 android 编程的初学者。我想在android中学习Fragment,我编写了一个简单的程序,但是当我想运行它时,程序出错了!为什么?

MainActivity.java:

public class MainActivity extends Activity {

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

    btn=(Button)findViewById(R.id.btn1);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent1=new     Intent(MainActivity.this,TestFragment.class);
            startActivity(intent1);
        }
    });
}
}

TestFragment.java:

public class TestFragment extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_for_btn1);

}
 }

ViewFragment.java:

public class ViewFragment extends Fragment{
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle   savedInstanceState){
    View v=inflater.inflate(R.layout.ui_for_fragment1, container, false);

    Button btnf1;
    btnf1=(Button)v.findViewById(R.id.btnf1);
    btnf1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "This is first fragment!!",  Toast.LENGTH_LONG).show();   
        }
    });
    return v;

}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
    android:id="@+id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="CLICK ME FOR TEST FRAGMENT!"/>
</LinearLayout>

fragment_for_btn1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Fragment
    android:id="@+id/fr1"
    android:name="com.example.fragmentexample.ViewFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
</LinearLayout> 

ui_for_fragment1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button
    android:id="@+id/btnf1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CLICK:)"></Button>
 </LinearLayout>

LogCat 是:

01-30 09:54:39.255: W/dalvikvm(14593): threadid=1: thread exiting with uncaught exception (group=0x4138e300)
01-30 09:54:39.270: E/AndroidRuntime(14593): FATAL EXCEPTION: main
01-30 09:54:39.270: E/AndroidRuntime(14593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentexample/com.example.fragmentexample.TestFragment}: android.view.InflateException: Binary XML file line #6: Error inflating class Fragment
01-30 09:54:39.270: E/AndroidRuntime(14593):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-30 09:54:39.270: E/AndroidRuntime(14593):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

【问题讨论】:

    标签: android fragment inflate-exception


    【解决方案1】:

    在您的 XML 文件中 fragment_for_btn1.xml:

    <Fragment
        android:id="@+id/fr1"
        android:name="om.example.fragmentexample.ViewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    

    应该是

    <fragment
        android:id="@+id/fr1"
        class="com.example.fragmentexample.ViewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    

    【讨论】:

    • 谢谢,我把 om 改成 com,但程序又出现错误 :(
    • 是的。错误再次出现 android.View.InflateException 。我编辑了我的第一个答案。
    • 感谢您的帮助。我写片段标签而不是片段。这很糟糕。
    • 另一个问题:为什么我不能在TestFragment类中扩展Activity而不是FragmentActivity?
    • Activity in API 10 下面不支持片段。
    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多