2

I'm getting a NullPointerException on my ArrayAdapter. I'm getting a value from a database connected to the localhost and When I post the value to the log cat and delete the textview.settext("something") my error is gone. But when I initialize the TextView and put something in it the error shows again.

Here is my snippet:

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;
        }
    }
}

My error is here:

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)

Please help thanks

4

4 回答 4

4

Replace

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

By

View rowView = inflater.inflate(R.layout.rowlayout, null);
于 2012-07-09T07:37:44.513 回答
0

Most probably, your textview or rowview is a null object :-)

Have you tried debugging it ?

EDIT

于 2012-07-09T07:38:18.690 回答
0

First you are Using viewholder in a bad way, Second use 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);
于 2012-07-09T07:47:21.360 回答
0

There's nothing wrong on your above code in order to run normally. Based on many empty lines, I guess you have tried to deleted some lines, update layout ..blah blah. So I think clean project and re-run it again.

Sometimes, when you try to edit layout and don't clean your projects, it will use the previous build cache, which usually cause NullPointerException because the new layout is not updated.

于 2012-07-09T07:47:46.863 回答