使用自定义数组适配器的NullPointerException

时间:2012-07-09 07:28:54

标签: android nullpointerexception

我的NullPointerException收到了ArrayAdapter。我从连接到localhost的数据库中获取值,当我将值发布到log cat并删除textview.settext("something")时,我的错误消失了。但是当我初始化TextView并在其中放入某些内容时,错误会再次出现。

这是我的代码:

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final List<String> values;
    //private final List<String> arr;

    public MySimpleArrayAdapter(Context context, List<String> values) {
        super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = values;
    }

    static class ViewHolder {
        public TextView text;
        public ImageView image;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("List",values.get(position).toString());
        LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder viewHolder = new ViewHolder();
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.txtlarge);
        textView.setText("hahhaha");

        return rowView;
    }

    private Drawable LoadImageFromWebOperations(String url)
    {
        try
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } 
        catch (Exception e)
        {
            Log.w("LoadImageFromWebOperations",e.toString());
            return null;
        }
    }
}

我的错误在这里:

07-09 02:20:47.433: E/AndroidRuntime(1168): FATAL EXCEPTION: main
07-09 02:20:47.433: E/AndroidRuntime(1168): java.lang.NullPointerException
07-09 02:20:47.433: E/AndroidRuntime(1168):     at com.database_demo.MySimpleArrayAdapter.getView(MySimpleArrayAdapter.java:57)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.ListView.fillDown(ListView.java:652)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.ListView.fillFromTop(ListView.java:709)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.ListView.layoutChildren(ListView.java:1580)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.View.layout(View.java:7035)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.View.layout(View.java:7035)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.View.layout(View.java:7035)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.View.layout(View.java:7035)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.os.Looper.loop(Looper.java:123)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invoke(Method.java:521)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at dalvik.system.NativeStart.main(Native Method)

请帮助谢谢

4 个答案:

答案 0 :(得分:4)

替换

View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

通过

View rowView = inflater.inflate(R.layout.rowlayout, null);

答案 1 :(得分:0)

最有可能的是,您的textviewrowview是一个空对象: - )

你试过调试吗?

修改

答案 2 :(得分:0)

首先,您正在以不好的方式使用视图,第二次使用convertView = inflater.inflate(R.layout.rowlayout, null);

  LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView ==null ) {

    convertView = inflater.inflate(R.layout.rowlayout, null);
    // Creates a ViewHolder and store references to the two children views
    // we want to bind data to.
    holder = new ViewHolder();
    convertView.setTag(holder);
} else {
    // Get the ViewHolder back to get fast access to the TextView
    // and the ImageView.
    holder = (ViewHolder) convertView.getTag();
}

// Bind the data efficiently with the holder.
holder.text = (TextView) convertView.findViewById(R.id.txtLarge);
holder.image = (ImageView) convertView.findViewById(R.id.icon);

答案 3 :(得分:0)

上述代码没有任何问题,无法正常运行。根据许多空行,我猜你已经尝试删除一些行,更新布局..blah blah。所以我认为干净的项目并重新运行它。

有时,当您尝试编辑布局而不清理项目时,它将使用之前的构建缓存,这通常会导致NullPointerException,因为新布局未更新。