我之前已经发布了一个问题,现在我没有错误但是当我尝试在我的模拟Android设备上运行我的应用程序时,它会出现一个错误,上面写着
抱歉应用程序Hello Grid View (进程com.HelloGridView)有 意外停了下来。请试试 试。
以下是文件稍后将遵循的文件树:
HelloGridView
src
com.HelloGridView
HelloGridView.java
ImageAdapter.java
gen
Android 2.2
assets
res
drawable( with all the pics)
HelloGridView.java
package com.HelloGridView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class HelloGridView 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 AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter.java
package com.HelloGridView;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public ImageAdapter(HelloGridView c) {
// TODO Auto-generated constructor stub
}
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) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// 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,
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,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
这是我的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"
/>
答案 0 :(得分:3)
如果从ImageAdapter类中删除这些行,一切都会正常工作。
public ImageAdapter(HelloGridView c) {
// TODO Auto-generated constructor stub
}
这个构造函数被调用,你的Context没有被设置。我不是Java专家,但我认为它被调用,因为它更具体。
您已经有了这个构造函数来处理设置上下文。
public ImageAdapter(Context c) {
mContext = c;
}
另一个解决方案是将现有的构造函数更改为此,它也可以正常工作。
public ImageAdapter(HelloGridView c) {
mContext = c;
}
答案 1 :(得分:1)
问题是由于本教程中的这一行 “将图像文件保存到项目的res / drawable /目录中。”
在你的项目中,你没有这个文件夹,而是/ drawable-hdpi(ldpi ......等)
所以解决它,只需在res目录下创建新文件夹并将其命名为“drawable”并将图像导入该文件夹。
OR
AndroidManifest.xml中的将“@ drawable / icon”行更新到您的文件夹。