使用baseadapter时老是出现空指针异常

yangweilinywl 发布于 2016/08/03 19:02
阅读 331
收藏 0
08-03 18:55:49.344: E/Trace(9183): error opening trace file: No such file or directory (2)
08-03 18:55:50.535: W/dalvikvm(9183): threadid=1: thread exiting with uncaught exception (group=0x40f883a8)
08-03 18:55:50.545: E/AndroidRuntime(9183): FATAL EXCEPTION: main
08-03 18:55:50.545: E/AndroidRuntime(9183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yang/com.example.yang.MainActivity}: java.lang.NullPointerException
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.app.ActivityThread.access$600(ActivityThread.java:151)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.os.Looper.loop(Looper.java:155)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.app.ActivityThread.main(ActivityThread.java:5520)
08-03 18:55:50.545: E/AndroidRuntime(9183): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 18:55:50.545: E/AndroidRuntime(9183): at java.lang.reflect.Method.invoke(Method.java:511)
08-03 18:55:50.545: E/AndroidRuntime(9183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
08-03 18:55:50.545: E/AndroidRuntime(9183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
08-03 18:55:50.545: E/AndroidRuntime(9183): at dalvik.system.NativeStart.main(Native Method)
08-03 18:55:50.545: E/AndroidRuntime(9183): Caused by: java.lang.NullPointerException
08-03 18:55:50.545: E/AndroidRuntime(9183): at com.example.yang.MainActivity$Myadapter.getCount(MainActivity.java:79)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.widget.ListView.setAdapter(ListView.java:577)
08-03 18:55:50.545: E/AndroidRuntime(9183): at com.example.yang.MainActivity.onCreate(MainActivity.java:39)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.app.Activity.performCreate(Activity.java:5066)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-03 18:55:50.545: E/AndroidRuntime(9183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
08-03 18:55:50.545: E/AndroidRuntime(9183): ... 11 more

加载中
0
nesger
nesger
日志中可以看到,是空指针异常。你到MainActivity.java的79行debug一下,看看是不是有那些值是null
0
咫尺已天涯
咫尺已天涯
经常的情况是 传入的数据集合为空,在 getCount 的时候返回list.size就空了
0
y
yangweilinywl

引用来自“nesger”的评论

日志中可以看到,是空指针异常。你到MainActivity.java的79行debug一下,看看是不是有那些值是null
这是我的部分代码,mNewsInfoList测试过了不是空的,后面是那个地方出现问题了,求指导。。
package com.example.yang;
import java.util.List;
import com.example.utils.News;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class Myadapter extends BaseAdapter {


private Context mcontext;
private LayoutInflater minflater;
private List<News> mNewsInfoList;

public Myadapter(Context context, LayoutInflater inflater,List<News> NewsInfoList) {
super();
this.mcontext = mcontext;
this.minflater = minflater;
this.mNewsInfoList = NewsInfoList;
System.out.println(mNewsInfoList.size());
}




@Override
public int getCount() {
// TODO 自动生成的方法存根
return mNewsInfoList.size();
}


public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder viewholder = null;
   News news = mNewsInfoList.get(position);
if (convertView==null) {
viewholder = new ViewHolder();
convertView = minflater.inflate(R.layout.listitem, null);
viewholder.tvTitle =(TextView) convertView.findViewById(R.id.title_view);
   viewholder.tvDetail = (TextView) convertView.findViewById(R.id.detail_view);
viewholder.tvComment = (TextView) convertView.findViewById(R.id.comment_view);
viewholder.ivUrl = (ImageView)convertView.findViewById(R.id.image_view_bitmap);
convertView.setTag(viewholder);
}
else {
viewholder = (ViewHolder) convertView.getTag();
}
viewholder.ivUrl.setImageURI(Uri.parse(news.getImageurl()));
viewholder.tvTitle.setText(news.getTitle());
viewholder.tvDetail.setText(news.getDetail());
viewholder.tvComment.setText(news.getComment());

return convertView;
}

@Override
public Object getItem(int position) {
// TODO 自动生成的方法存根
return null;
}


@Override
public long getItemId(int position) {
// TODO 自动生成的方法存根
return 0;
}


  
}


0
nesger
nesger

引用来自“nesger”的评论

日志中可以看到,是空指针异常。你到MainActivity.java的79行debug一下,看看是不是有那些值是null

引用来自“yangweilinywl”的评论

这是我的部分代码,mNewsInfoList测试过了不是空的,后面是那个地方出现问题了,求指导。。
package com.example.yang;
import java.util.List;
import com.example.utils.News;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class Myadapter extends BaseAdapter {


private Context mcontext;
private LayoutInflater minflater;
private List<News> mNewsInfoList;

public Myadapter(Context context, LayoutInflater inflater,List<News> NewsInfoList) {
super();
this.mcontext = mcontext;
this.minflater = minflater;
this.mNewsInfoList = NewsInfoList;
System.out.println(mNewsInfoList.size());
}




@Override
public int getCount() {
// TODO 自动生成的方法存根
return mNewsInfoList.size();
}


public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder viewholder = null;
   News news = mNewsInfoList.get(position);
if (convertView==null) {
viewholder = new ViewHolder();
convertView = minflater.inflate(R.layout.listitem, null);
viewholder.tvTitle =(TextView) convertView.findViewById(R.id.title_view);
   viewholder.tvDetail = (TextView) convertView.findViewById(R.id.detail_view);
viewholder.tvComment = (TextView) convertView.findViewById(R.id.comment_view);
viewholder.ivUrl = (ImageView)convertView.findViewById(R.id.image_view_bitmap);
convertView.setTag(viewholder);
}
else {
viewholder = (ViewHolder) convertView.getTag();
}
viewholder.ivUrl.setImageURI(Uri.parse(news.getImageurl()));
viewholder.tvTitle.setText(news.getTitle());
viewholder.tvDetail.setText(news.getDetail());
viewholder.tvComment.setText(news.getComment());

return convertView;
}

@Override
public Object getItem(int position) {
// TODO 自动生成的方法存根
return null;
}


@Override
public long getItemId(int position) {
// TODO 自动生成的方法存根
return 0;
}


  
}


@Override
public Object getItem(int position) {
// TODO 自动生成的方法存根
return null;
}


@Override
public long getItemId(int position) {
// TODO 自动生成的方法存根
return 0;

}

你这里的两个return值都是有问题的。

改为:

@Override
public Object getItem(int position) {
// TODO 自动生成的方法存根
return mNewsInfoList.get(position);
}


@Override
public long getItemId(int position) {
// TODO 自动生成的方法存根
return position;

}


0
火文乐武
火文乐武
给你的数据加上一个判断是否为空的操作,如果是空的,填充默认数据,不是空的再走adapter这个逻辑,主要还是你的数据有问题
返回顶部
顶部