Android Eclipse运行TIme错误InflateException

时间:2014-12-30 01:50:09

标签: android eclipse android-layout

我工作了10个小时,但没有理解问题出在哪里。 我尝试了一切,虽然有不同的stackoverflow帖子有类似的问题,我找不到一个解决我的情况,我有一个直觉,因为这行执行时出现错误 View view = inflater.inflate(R.layout.frag1,container,false); 调试器证实了我的意思。谢谢你的关注

这是DDMS

12-29 20:04:52.908: D/MainActivity(2293): onCreate: 14
12-29 20:05:33.638: D/Frag1(2293): onCreateView: 28
12-29 20:13:01.778: D/MainActivity(2341): onCreate: 14
12-29 20:13:02.068: D/Frag1(2341): onCreateView: 28
12-29 20:13:02.118: D/AndroidRuntime(2341): Shutting down VM
12-29 20:13:02.118: W/dalvikvm(2341): threadid=1: thread exiting with uncaught exception (group=0xb3ad7ba8)
12-29 20:13:02.188: E/AndroidRuntime(2341): FATAL EXCEPTION: main
12-29 20:13:02.188: E/AndroidRuntime(2341): Process: com.example.ciao, PID: 2341
12-29 20:13:02.188: E/AndroidRuntime(2341): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ciao/com.example.ciao.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.os.Looper.loop(Looper.java:136)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at java.lang.reflect.Method.invoke(Method.java:515)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at dalvik.system.NativeStart.main(Native Method)
12-29 20:13:02.188: E/AndroidRuntime(2341): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.Activity.setContentView(Activity.java:1929)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at com.example.ciao.MainActivity.onCreate(MainActivity.java:17)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.Activity.performCreate(Activity.java:5231)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-29 20:13:02.188: E/AndroidRuntime(2341):     ... 11 more
12-29 20:13:02.188: E/AndroidRuntime(2341): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.view.ViewGroup
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at com.example.ciao.Frag1.onCreateView(Frag1.java:27)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.Fragment.performCreateView(Fragment.java:1700)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.app.Activity.onCreateView(Activity.java:4786)
12-29 20:13:02.188: E/AndroidRuntime(2341):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
12-29 20:13:02.188: E/AndroidRuntime(2341):     ... 21 more
12-29 20:13:06.248: I/Process(2341): Sending signal. PID: 2341 SIG: 9

这是活动主要

<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:background="@android:color/white" >

    <fragment
        android:id="@+id/producta"
        android:name="com.example.ciao.Frag1"
        android:layout_width="wrap_content"


        android:layout_height="wrap_content" />

</RelativeLayout>

这是frag1.xml

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/ciao"
        android:padding="10dp" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="@string/hello_world"

            android:padding="10dp" />
    </ImageView>

</RelativeLayout>

这是Frag1.java

package com.example.ciao;

import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
import android.widget.TextView;

public class Frag1 extends Fragment{

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
Log.d("Frag1", "onCreateView: 28");

    View view =inflater.inflate(R.layout.frag1, container, false);
    Log.d("Frag1", "onCreateView: 30");

    ImageView imageView=(ImageView) view.findViewById(R.id.image);
Log.d("Frag1", "onCreateView: 33");
TextView textView= (TextView) view.findViewById(R.id.title);
Log.d("Frag1", "onCreateView: 35");

Drawable icon =getResources().getDrawable(R.drawable.ic_launcher);
Log.d("Frag1", "onCreateView: 38");
imageView.setImageDrawable(icon);
Log.d("Frag1", "onCreateView: 40");
textView.setText("Mastering Java swing");
Log.d("Frag1", "onCreateView: 42");
return view;
}

@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

这是主要活动

package com.example.ciao;

//import android.support.v4.app.FragmentActivity;
//import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("MainActivity", "onCreate: 14");
    setContentView(R.layout.activity_main);
    Log.d("MainActivity", "onCreate: 16");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    Log.d("MainActivity", "onCreateOptionsMenu: 22");
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:1)

frag1.xml中,您有一个ImageView,其中包含另一个元素:

<ImageView ...>
    <TextView ... />
</ImageView>

你做不到。 ImageView和TextView必须是对等的:

<ImageView ... />
<TextView ... />

如果您尝试将textview放在imageview的顶部,请使用允许您执行此操作的RelativeLayout parent。