我已经检查了堆栈上的大多数解决方案是否存在此错误,但遗憾的是无法解决我的问题。以下是我面临此错误的代码
public class ImageGrid extends BaseActivity {
private GridView grid;
private MyAdapter mAdapter;
private DBAdapter mdbAdapter;
private DisplayImageOptions options;
DBAdapter localDBAdapter;
Button play;
String str, strid;
String delay;
TextView tv1;
private Uri media;
Cursor localCursor2, imageCursor;
ArrayList<String> localArrayList, imageArray;
private RadioGroup effects;
private RadioGroup more;
private RadioButton effectsbtn;
private RadioButton morebtn;
private int effectNo;
DraggableGridView images;
private FullImageLoader mLoder ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_grid);
this.strid = getIntent().getStringExtra("boardid");
this.media=getIntent().getData();
this.mdbAdapter = new DBAdapter(getBaseContext());
this.options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_gallery).showImageForEmptyUri(R.drawable.ic_gallery).cacheInMemory().cacheOnDisc().build();
TabHost tabs=(TabHost)findViewById(android.R.id.tabhost);
tabs.setup();
TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.slidetab);
spec.setIndicator("Slideshow");
tabs.addTab(spec);
TabSpec spec2=tabs.newTabSpec("tag2");
spec2.setContent(R.id.edittab);
spec2.setIndicator("Edit");
tabs.addTab(spec2);
images = (DraggableGridView)findViewById(R.id.editGrid);
this.imageCursor = this.mdbAdapter.getShowCursor(strid);
this.imageArray = new ArrayList<String>();
imageCursor.moveToFirst();
imageArray.add(imageCursor.getString(imageCursor.getColumnIndex("url")));
while (imageCursor.moveToNext())
{
imageArray.add(imageCursor.getString(imageCursor.getColumnIndex("url")));
}
int i = this.imageArray.size();
for (int j = 0; j < i; j++)
{
ImageView localImageView1 = new ImageView(ImageGrid.this);
localImageView1.setScaleType(ImageView.ScaleType.CENTER_CROP);
localImageView1.setImageBitmap(ImageGrid.this.mLoder.getBitmap((String)this.imageArray.get(j), 300, 300));
ImageGrid.this.images.addView(localImageView1);
//Toast.makeText(getApplicationContext(), " url" + imageArray.get(j), Toast.LENGTH_LONG).show();
}
grid =(GridView)findViewById(R.id.photogrid);
this.mAdapter = new MyAdapter(this, this.mdbAdapter.getShowCursor(strid));
this.grid.setAdapter(this.mAdapter);
错误在localImageView1.setImageBitmap(ImageGrid.this.mLoder.getBitmap((String)this.imageArray.get(j), 300, 300));
我的logcat如下
11-28 11:26:10.646: E/AndroidRuntime(23972): FATAL EXCEPTION: main
11-28 11:26:10.646: E/AndroidRuntime(23972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application1/com.example.application1.ImageGrid}: java.lang.NullPointerException
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.app.ActivityThread.access$600(ActivityThread.java:149)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.os.Looper.loop(Looper.java:153)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.app.ActivityThread.main(ActivityThread.java:5086)
11-28 11:26:10.646: E/AndroidRuntime(23972): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 11:26:10.646: E/AndroidRuntime(23972): at java.lang.reflect.Method.invoke(Method.java:511)
11-28 11:26:10.646: E/AndroidRuntime(23972): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
11-28 11:26:10.646: E/AndroidRuntime(23972): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
11-28 11:26:10.646: E/AndroidRuntime(23972): at dalvik.system.NativeStart.main(Native Method)
11-28 11:26:10.646: E/AndroidRuntime(23972): Caused by: java.lang.NullPointerException
11-28 11:26:10.646: E/AndroidRuntime(23972): at com.example.application1.ImageGrid.onCreate(ImageGrid.java:109)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.app.Activity.performCreate(Activity.java:5020)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-28 11:26:10.646: E/AndroidRuntime(23972): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-28 11:26:10.646: E/AndroidRuntime(23972): ... 11 more
xml文件是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/page3">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<LinearLayout
android:id="@+id/slidetab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<GridView
android:gravity="center"
android:id="@+id/photogrid"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:horizontalSpacing="4.0dip"
android:verticalSpacing="10.0dip"
android:stretchMode="columnWidth"
android:columnWidth="100.0dip"
android:numColumns="2"
android:layout_weight="1.0" />
<Button
android:layout_gravity="center"
android:id="@+id/play"
android:background="@drawable/slideshow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/edittab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:id="@+id/addImage"
android:text="Add Images"
android:layout_marginLeft="10dp"></Button>
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:id="@+id/addMusic"
android:text="Add Images"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.application1.views.DraggableGridView
android:id="@+id/editGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical"
android:background="#CCCCCC"
/>
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Update"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
我无法找到解决方案,请帮忙。谢谢。
答案 0 :(得分:1)
在任何地方使用之前,只需初始化 mLoder 即可。