使用gridlayout获取Inflate Exception

时间:2011-04-08 05:34:12

标签: android android-layout

嗨,大家好我得到了android.view.InflateException:二进制XML文件行#1:错误膨胀类linearlayout,引起:java.lang.ClassNotFoundException:android.view.linearlayout in loader dalvik.system.PathClassLoader [/ data /app/com.project.news-2.apk]在下面的指示行中。

看看并

HomeActivity

public void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

}

HomeActivity内的另一种方法

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.dashboard_home ,(ViewGroup) findViewById(R.id.layout_root));


GridView grid = (GridView) layout.findViewById(R.id.gridview);


adapter = new HomeAdapter(this);

adapter.setItems(config); grid.setAdapter(adapter);

HomeAdapter

public View getView(int position, View convertView, ViewGroup parent) {



ViewHolder holder

if (convertView == null) { // if it's not recycled,



 convertView = mInflater.inflate(R.layout.dashboard_home, null);  //exception

convertView.setLayoutParams(new GridView.LayoutParams(90, 90));


holder = new ViewHolder();

holder.title = (TextView) convertView.findViewById(R.id.titleIcon);

holder.icon = (ImageView) convertView.findViewById(R.id.imageIcon);

convertView.setTag(holder);


} else {

 holder = (ViewHolder) convertView.getTag();

       }

main.xml

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/layout_root" android:orientation="vertical"

    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <LinearLayout style="@style/TitleBar">

    <ImageView style="@style/TitleBarLogo"

    android:contentDescription="Text testing" />



<ImageView style="@style/TitleBarSeparator" />

<TextView style="@style/TitleBarText" />



<ImageButton style="@style/TitleBarAction"

android:contentDescription="@string/description_about" android:src="@drawable/info"

android:onClick="onClickAbout" />

</LinearLayout>



<GridView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:id="@+id/gridview"

    android:numColumns="2"

    />



</LinearLayout>

dashboard_home.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" android:padding="10dp">

<ImageView android:id="@+id/imageIcon" android:layout_width="50dip"

android:layout_height="50dip" android:layout_alignParentRight="true"

android:layout_marginRight="3dp" />

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

android:layout_height="wrap_content" android:layout_centerHorizontal="true"

android:layout_alignParentLeft="true" android:layout_marginLeft="3dp"

android:textColor="#FFF" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

删除这两行并在HomeActivity中运行您的代码。

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);     
View layout = inflater.inflate(R.layout.dashboard_home ,(ViewGroup) findViewById(R.id.layout_root));  

异常发生的原因是因为你将dashboard_home.xml膨胀为自身......