它以前工作过,但是由于未知的原因,即使一切都很好,我现在仍然有nullpointer异常。自己看看是否可以提供帮助:
public class TestActivity extends ListActivity {
ArrayList<String> array;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array = getsms();
SMSadapter adapter = new SMSadapter(this, array);
getListView().setAdapter(adapter);
}
public class SMSadapter extends BaseAdapter {
Context mcontext;
ArrayList<String> marray;
LayoutInflater inflater;
public SMSadapter (Context context, ArrayList<String>array){
mcontext = context;
marray = array;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return marray.size();
}
@Override
public Object getItem(int position) {
return marray.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
TextView text;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if (convertView == null){
convertView = inflater.inflate(R.layout.items, null);
holder.text = (TextView)findViewById(R.id.text);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.text.setText(marray.get(position));
return convertView;
}
}
public ArrayList<String> getsms(){
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[]{"address"}, null,null,null);
cursor.moveToFirst();
ArrayList<String> temp = new ArrayList<String>();
while(cursor.moveToNext()){
temp.add(cursor.getString(0));
}
return temp;
}
}
12-17 16:11:17.037: E/AndroidRuntime(558): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.View.measure(View.java:8313)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.View.measure(View.java:8313)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.View.measure(View.java:8313)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.os.Looper.loop(Looper.java:123)
12-17 16:11:17.037: E/AndroidRuntime(558): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-17 16:11:17.037: E/AndroidRuntime(558): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 16:11:17.037: E/AndroidRuntime(558): at java.lang.reflect.Method.invoke(Method.java:507)
12-17 16:11:17.037: E/AndroidRuntime(558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-17 16:11:17.037: E/AndroidRuntime(558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-17 16:11:17.037: E/AndroidRuntime(558): at dalvik.system.NativeStart.main(Native Method)
12-17 16:22:06.778: D/AndroidRuntime(587): Shutting down VM
12-17 16:22:06.778: W/dalvikvm(587): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-17 16:22:06.788: E/AndroidRuntime(587): FATAL EXCEPTION: main
12-17 16:22:06.788: E/AndroidRuntime(587): java.lang.NullPointerException
12-17 16:22:06.788: E/AndroidRuntime(587): at com.test.TestActivity$SMSadapter.getView(TestActivity.java:74)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.ListView.onMeasure(ListView.java:1127)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.os.Looper.loop(Looper.java:123)
12-17 16:22:06.788: E/AndroidRuntime(587): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-17 16:22:06.788: E/AndroidRuntime(587): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 16:22:06.788: E/AndroidRuntime(587): at java.lang.reflect.Method.invoke(Method.java:507)
12-17 16:22:06.788: E/AndroidRuntime(587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-17 16:22:06.788: E/AndroidRuntime(587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-17 16:22:06.788: E/AndroidRuntime(587): at dalvik.system.NativeStart.main(Native Method)
最佳答案
您有此错误:
holder.text = (TextView)findViewById(R.id.text);
相反,您必须这样做:
holder.text = (TextView)convertView.findViewById(R.id.text);
希望这可以帮助!
关于android - 为什么在getView()中使用LayoutInflater不能使BaseAdapter正常工作?它不断返回nullpointer异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8546103/