我所做的一切
我在布局中创建了一个网格视图。这个网格视图我想填充我创建的google文档的imageadapter:Hello GridView
这张照片是我的照片的一页,我是否存进了数据库,我想把它填在上面。因为在我的datbase中,所有东西都是我在活动中编写的一个字符串,它是一个短函数,可以将字符串[]更改为in t[],但我不能通过imageadapter访问它。后来,我把尼基塔夫人告诉我的那些变化都应用了。另外,我更改为lil thing,将字符串[]更改为int[](现在正在工作)。
问题
要将int[]导入到imageadapter并在grindview中显示图片,我需要更改什么。
现在有一个空指针异常:
public int getCount() {
return mThumbIds.length;
}
代码:
这是我的smileyconfigactivity.java的代码:
package de.retowaelchli.filterit;
import de.retowaelchli.filterit.database.SmileyDBAdapter;
import de.retowaelchli.filterit.stats.ImageAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
public class SFilterConfigActivity extends Activity {
//Variablen deklarieren
private String name;
private String keyword;
private Integer[] info;
private SmileyDBAdapter SmileyHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sfilter_config);
SmileyHelper = new SmileyDBAdapter(this);
getImages();
}
public Integer[] getImages(){
SmileyHelper.open();
Cursor c = SmileyHelper.getAllSmileys();
startManagingCursor(c);
String[] picture = new String[] { SmileyDBAdapter.INFO };
int i;
for (i=0;i< picture.length-1;i++) {
info[i]=Integer.parseInt(picture[i+1]);
}
SmileyHelper.close();
//Hier wird die Grindview gefüllt
GridView gridview = (GridView) findViewById(R.id.SmileyGrind);
gridview.setAdapter(new ImageAdapter(this, info));
return info;
}
public void onClickSConfigSave(View v){
EditText edtTextName = (EditText)findViewById(R.id.SFConfigName);
EditText edtTextKeyword = (EditText)findViewById(R.id.SFConfigKeyword);
name = edtTextName.getText().toString();
keyword = edtTextKeyword.getText().toString();
final Intent i = new Intent(this, SmileyActivity.class);
startActivity(i);
}
}
这是我的imageadapter.java的代码:
package de.retowaelchli.filterit.stats;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import de.retowaelchli.filterit.SFilterConfigActivity;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c, Integer[] imageIds) {
mContext = c;
mThumbIds = imageIds;
}
//This Guy down here is giving me the NullpointerExcpetion now
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;
}
这就是我如何将图片存储到数据库(我只保存到它们所在的位置):
int drawableID = context.getResources().getIdentifier("angel", "drawable", getPackageName());
iv.setImageResource(drawableID);
String info = String.valueOf(drawableID);
mDbHelper.open();
mDbHelper.createSmiley("You have received a heavenly message", info);
mDbHelper.close();
这是我得到的错误日志:
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): FATAL EXCEPTION: main
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.retowaelchli.filterit/de.retowaelchli.filterit.SFilterConfigActivity}: java.lang.NullPointerException
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.os.Looper.loop(Looper.java:143)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.main(ActivityThread.java:4196)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at java.lang.reflect.Method.invoke(Method.java:507)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at dalvik.system.NativeStart.main(Native Method)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): Caused by: java.lang.NullPointerException
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at de.retowaelchli.filterit.stats.ImageAdapter.getCount(ImageAdapter.java:25)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.widget.GridView.setAdapter(GridView.java:131)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at de.retowaelchli.filterit.SFilterConfigActivity.getImages(SFilterConfigActivity.java:54)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at de.retowaelchli.filterit.SFilterConfigActivity.onCreate(SFilterConfigActivity.java:30)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): ... 11 more
谢谢你的帮助!
最佳答案
这里似乎有很多相关的信息,但是如果你填空,我会更新答案。第一个问题是在getAllSmileys函数中检索什么?
您需要了解的是,游标就像结果的内存表。因此,您需要通过使用move命令来告诉游标要从哪个记录中提取数据,否则它是未平铺的。因此,假设您的smilleys存储在smileydbapter.info列中,那么您可以这样检索它们:
SmileyHelper.open();
Cursor c = SmileyHelper.getAllSmileys();
if(c!=null)
{
int i=0;
info = new int[c.getCount()]();
//get the column index
int infoIndex = c.getColumnIndex(SmileyDBAdapter.INFO);
while(c.moveToNext()){
String infoItem = c.getString(infoIndex );
int infoItemInteger = Integer.parseInt(infoItem);
info[i++] =infoItemInteger;
}
}
c.close();
SmileyHelper.close();
您将图像id以字符串的形式存储在数据库中,这使得事情更加复杂。不需要在数据库的文本列中解析和计算drawable resource id的值。只需将列类型更改为整数,并始终使用图像的整数。
您可以简单地使用cursor.getint()来检索值。