简体   繁体   中英

Android: ListView Load gives Null Pointer Exception

I'm trying to get data from sqlite to listview so i followed the example in the answer in this url Make listview from SQLite database

the CustomCursorAdapter is going like that

    public class CustomCursorAdapter extends SimpleCursorAdapter  {
    private int layout; 
    private LayoutInflater inflater;
    private Context context;

    public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.layout = layout;
        this.context = context;
        inflater = LayoutInflater.from(context);

    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        View v = inflater.inflate(R.layout.topics, parent, false);

        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {
                //1 is the column where you're getting your data from
        String name = c.getString(1);
        /**
         * Next set the name of the entry.
         */
        TextView name_text = (TextView) v.findViewById(R.id.listLabel);
        if (name_text != null) {
            name_text.setText(name);
        }   
    }
}

but onCreate i have this code

ListView listView = (ListView)findViewById(R.layout.topics);
    connectToDB();
    from = new String[] { "topicTitle" };
    to = new int[] { R.id.listLabel };

    CustomCursorAdapter adapter = new CustomCursorAdapter(this,
            R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);

with xml as follows

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/listLogo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
         >
    </ImageView>

    <TextView
        android:id="@+id/listLabel"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="15sp" >
    </TextView>

</LinearLayout>

the problem is .. listView is Null!! .. which i don't know why!!

You are refering to your layout topics. You should have a listview defined in xml with a id. In your activity onCreate() after setCOntentView(R.layout.topics), fins the id of listview and set adapter to your list.

In your xml say topics.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#0095FF">
   <ListView android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="100dp" 
    android:id="@+id/lv">
   </ListView>
    // other ui elements
</LinearLayout>

Then in your activity onCreate()

   setContentview(R.layout.topics);
   ListView listView = (ListView)findViewById(R.id.lv);
   connectToDB();
   from = new String[] { "topicTitle" };
   to = new int[] { R.id.listLabel };


   CustomCursorAdapter adapter = new CustomCursorAdapter(this,
        R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);

From the discussion in chat

EDIT:

Define main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#0095FF">

<ListView android:id="@+id/list"
android:layout_width="fill_parent"  
android:layout_height="0dip"
android:focusableInTouchMode="false"
android:listSelector="@android:color/transparent"
android:layout_weight="2"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:dividerHeight="8dp" 
android:divider="#000000" 
android:cacheColorHint="#000000"
android:drawSelectorOnTop="false">
</ListView>
</LinearLayout>

In your activivity

 public class MainActivity extends Activity {

 ListView lv;
 CustomCursorAdapter cus;
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv = (ListView) findViewById(R.id.list);
    cus= new CustomCursorAdapter(parameters);
    lv.setAdapter(cus);

 }
 }

Define customCursor adapter inflate a custom layout. Set image for image view and text for textview.

Try to edit your xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" 
android:id="@+id/main"
>

<ImageView
    android:id="@+id/listLogo"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
     >
</ImageView>

<TextView
    android:id="@+id/listLabel"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="15sp" >
</TextView>

<ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/topics"
        ></ListView>

 </LinearLayout>

In Activity onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView listView = (ListView)findViewById(R.layout.topics);
    connectToDB();
    from = new String[] { "topicTitle" };
    to = new int[] { R.id.listLabel };

    CustomCursorAdapter adapter = new CustomCursorAdapter(this,
            R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM