problems with running hellogridview from android.com
I have posted a question before about this now I have no errors but when I try to run my app on my simulated android device it comes up with an error that says
sorry the application Hello Grid View (process com.HelloGridView) has stopped unexpectedly. Please try again.
Here is the file tree the files will follow later:
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;
开发者_StackOverflowimport 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
};
}
here is my 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"
/>
If you remove these lines from your ImageAdapter class everything will work.
public ImageAdapter(HelloGridView c) {
// TODO Auto-generated constructor stub
}
This constructor is getting called and your Context is not getting set. I'm not a Java expert but I assume it is getting called because it is more specific.
You already have this constructor which will handle setting up the context.
public ImageAdapter(Context c) {
mContext = c;
}
Another solution is to change your existing constructor to this and it will work too.
public ImageAdapter(HelloGridView c) {
mContext = c;
}
The problem is because of this line in the tutorial "Save the image files into the project's res/drawable/ directory."
In your project you dont have this folder but instead /drawable-hdpi (ldpi...etc.)
so solve it , just create new folder under the res directory and call it "drawable" and import your images to that folder.
OR
in the AndroidManifest.xml update the line "@drawable/icon" to your folder.
精彩评论