在我的应用程序中,我想在网格视图中显示图像下方的文本。
以下是代码,但它不起作用。它显示了像这样的例外
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): FATAL EXCEPTION: main
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): android.view.InflateException: Binary XML file line #1: Error inflating class linearlayout
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at sra.gri.vie.Main$ImageAdapter.getView(Main.java:63)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.GridView.onMeasure(GridView.java:932)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.os.Looper.loop(Looper.java:123)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.reflect.Method.invoke(Method.java:521)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at dalvik.system.NativeStart.main(Native Method)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): Caused by: java.lang.ClassNotFoundException: android.view.linearlayout in loader dalvik.system.PathClassLoader[/data/app/sra.gri.vie-1.apk]
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): ... 26 more
05-31 17:23:32.017: INFO/ActivityManager(60): Process sra.gri.vie (pid 1262) has died.
grid_item.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#000080">
<imageview android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</imageview>
<textview android:id="@+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColor="#000000">
</textview>
</linearlayout>
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Main.java
package sra.gri.vie;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View MyView = convertView;
if (convertView == null) { // if it's not recycled, initialize some attributes
//Inflate the layout
LayoutInflater li = getLayoutInflater();
MyView = li.inflate(R.layout.grid_item, null);
// Add The Image!!!
ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
tv.setText(names[position] );
}
return MyView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
};
private String[] names={"Dog1","Dog2","Dog3","Dog4","Dog5","Dog6","Dog7","Dog8",};
}
}
感谢
答案 0 :(得分:2)
将元素linearlayout
重命名为LinearLayout
我有同样的问题,并以这种方式修复。
答案 1 :(得分:0)
错误抱怨了linearlayout(android.view.linearlayout)的错误包。正确的是android.widget.LinearLayout。但我在代码中找不到任何线性布局的引用。如果您可以粘贴完整的异常堆栈跟踪,这可能会有所帮助。