我正在尝试创建一个简单的列表视图,它使用自定义图像和字符串数组中的字符串,但是当我来运行它时,它会给出一个空指针异常,说存储= null。我不知道发生了什么,我已经三次检查了图像的所有拼写等等......并且无法找到任何可能在代码中引起的内容?
对于图像,我在res文件夹中创建了一个名为drawable的新目录并将它们放在那里,我还将它们添加到其他可绘制文件夹中以保证安全。
这是错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whosaidthat.endos.customimagelistview/com.whosaidthat.endos.customimagelistview.MyActivity}: java.lang.NullPointerException: storage == null
这是主要活动
package com.whosaidthat.endos.customimagelistview;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MyActivity extends ActionBarActivity {
ListView list;
String[] memeTitles;
String[] memeDescriptions;
int[] memeImages = {R.drawable.meme1, R.drawable.meme2, R.drawable.meme3, R.drawable.meme4, R.drawable.meme5, R.drawable.meme6, R.drawable.meme7, R.drawable.meme8, R.drawable.meme9, R.drawable.meme10,R.drawable.meme1, R.drawable.meme2, R.drawable.meme3, R.drawable.meme4, R.drawable.meme5, R.drawable.meme6, R.drawable.meme7, R.drawable.meme8, R.drawable.meme9, R.drawable.meme10};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Resources res = getResources();
res.getStringArray(R.array.titles);
res.getStringArray(R.array.descriptions);
list = (ListView) findViewById(R.id.listView);
lvAdapter adapter = new lvAdapter(this, memeTitles, memeImages, memeDescriptions);
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
class lvAdapter extends ArrayAdapter<String>{
int size=1;
Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
lvAdapter(Context c, String[] titles, int imgs[], String[] descriptions){
super(c, R.layout.single_row, R.id.textView, titles);
this.context = c;
this.images=imgs;
this.titleArray = titles;
this.descriptionArray = descriptions;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Converts the xml based single row into a java based view that we can manipulate and use
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
myImage.setImageResource(images[position]);
TextView myTitle = (TextView) row.findViewById(R.id.textView);
myTitle.setText(titleArray[position]);
TextView myDescription = (TextView) row.findViewById(R.id.textView2);
myDescription.setText(descriptionArray[position]);
return row;
}
}
如果您还有其他需要,请告诉我
由于
修改
以下是异常的整个logcat
07-27 10:15:03.466 1261-1261/com.whosaidthat.endos.customimagelistview D/dalvikvm﹕ Late-enabling CheckJNI
07-27 10:15:04.056 1261-1261/com.whosaidthat.endos.customimagelistview D/AndroidRuntime﹕ Shutting down VM
07-27 10:15:04.056 1261-1261/com.whosaidthat.endos.customimagelistview W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416e8ba8)
07-27 10:15:04.076 1261-1261/com.whosaidthat.endos.customimagelistview E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.whosaidthat.endos.customimagelistview, PID: 1261
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whosaidthat.endos.customimagelistview/com.whosaidthat.endos.customimagelistview.MyActivity}: java.lang.NullPointerException: storage == null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:141)
at com.whosaidthat.endos.customimagelistview.lvAdapter.<init>(MyActivity.java:67)
at com.whosaidthat.endos.customimagelistview.MyActivity.onCreate(MyActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
07-27 10:15:10.186 1261-1261/com.whosaidthat.endos.customimagelistview I/Process﹕ Sending signal. PID: 1261 SIG: 9
答案 0 :(得分:1)
将变量发送到适配器
时,变量为空String[] memeTitles;
String[] memeDescriptions;
我想你忘了这样做:
String[] memeTitles = res.getStringArray(R.array.titles);
String[] memeDescriptions = res.getStringArray(R.array.descriptions);