求大神解答,自定义baseAdapter报错

这是我的源码

package com.example.administrator.robot;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
//String[] msgStr=new String[20];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);

   /* for(int i=0;i<=19;i++)
        msgStr[i]=i+" ";*/
    ListView listView=(ListView)findViewById(R.id.list);
    BaseAdapter baseAdapter;
    baseAdapter = new MyAdapter(this);
    listView.setAdapter(baseAdapter);

}
public class ViewHolder{
    TextView textView;
}
private class MyAdapter extends BaseAdapter{
    private LayoutInflater layoutInflater;
    private ViewHolder viewHolder;

    public MyAdapter(Context context){
         layoutInflater=LayoutInflater.from(context);
         viewHolder=new ViewHolder();
    }
    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e("Tag","1");
        if(convertView==null){
            Log.e("Tag","1");
            convertView=layoutInflater.inflate(R.layout.item_test,null);
            viewHolder.textView=(TextView)findViewById(R.id.test_text);
            convertView.setTag(viewHolder);
            Log.e("Tag","1");
        }
        else{
            Log.e("Tag","1");
            viewHolder=(ViewHolder)convertView.getTag();
        }
        Log.e("Tag","1");
        viewHolder.textView.setText(position+"");
        Log.e("Tag","1");
        return convertView;
    }

}

}


点击运行后,没有出现界面就闪退了,报错。
04-02 12:24:47.305 20818-20818/com.example.administrator.robot E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.administrator.robot.MainActivity$MyAdapter.getView(MainActivity.java:75)
at android.widget.AbsListView.obtainView(AbsListView.java:2367)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
at android.widget.ListView.onMeasure(ListView.java:1162)
at android.view.View.measure(View.java:15733)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5052)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15733)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5052)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15733)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5052)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15733)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5052)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298)
at android.view.View.measure(View.java:15733)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2230)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1314)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1508)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1204)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4951)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5383)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)

7个回答

Log.e("Tag","1");
ViewHolde view holde = null;
if(convertView==null){
Log.e("Tag","1");
holde = new ViewHolder;

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e("Tag","1");
        if(convertView==null){
            Log.e("Tag","1");
            convertView=layoutInflater.inflate(R.layout.item_test,null);
                        viewHolder = new ViewHolder();//加上这句
            viewHolder.textView=(TextView)findViewById(R.id.test_text);
            convertView.setTag(viewHolder);
            Log.e("Tag","1");
        }
        else{
            Log.e("Tag","1");
            viewHolder=(ViewHolder)convertView.getTag();
        }
        Log.e("Tag","1");
        viewHolder.textView.setText(position+"");
        Log.e("Tag","1");
        return convertView;
    }

另外定义viewholder类时,加上static,这样资源使用会更少
http://www.eoeandroid.com/thread-302891-1-1.html

adapter的构造函数中不要创建holder,应放在getView中,如楼上两个演示的那样,应该创建了几次contentView就创建几次holder

@Override
public Object getItem(int position) {
return null;
}
这里不要返回null,不然容易犯错的....

viewHolder=new ViewHolder(); 这个放到getView中

_____myl 正解,ViewHold是一个class

像这种空指针异常(对象未实例化或对象对象而使用对象),错误信息里很明显报出了(MainActivity.java 75)直接点击进去就知道了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐