Listview Yielding Nullpointerexception On Setonitemclicklistener
Solution 1:
ListActivity doesn't require you to assign a layout via setContentView() that is if you want to show only a list but if you add a another view, your ListView should contain the android:id attribute set to @android:id/list like this xml below
<ListViewandroid:id="@android:id/list"android:layout_width="match_parent"android:layout_height="wrap_content" ></ListView>try changing your code
ListView lv = (ListView)findViewById(R.id.list);
to this
ListViewlv= getListView();
lv.setOnItemClickListener(newsSelectListener);
Solution 2:
I am not sure your code will work
privateOnItemClickListenernewsSelectListener=newOnItemClickListener()
{
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder alert=newAlertDialog.Builder(RssActivity.this);
alert.setTitle("Clicked").setMessage("Item clicked").setNeutralButton("OK", null).show();
}
};
But in my opinion, I often add setOnClickListener() to convertView in getView method in Adapter class
public View getView(finalint position, View convertView, ViewGroup parent) {
......................
convertView.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub//Do Somethings in here
}
}
});
Solution 3:
I cannot see where you have initialized your layout...so:
Activityclass takes care of creating a window for you in which you can place your UI withsetContentView(View).
The onCreate(Bundle) method initializes your Activity. It is where you usually call setContentView(int) with your xml layout(main.xml or your xml which defines your UI). Place it after super.onCreate(..)
With regards with this exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{}:
check your AndroidManifest.xml if your Activity is already in there:
<activityandroid:name=".<ActivityName>"android:label="@string/app_name"></activity>If your problem has not been resolved, make use of your Logcat and put Log.d in your methods to see where it's crashing.
Solution 4:
EDIT
Ok - and where are you setting setContentView(...)? The lv
ListViewlv= (ListView) findViewById(R.id.list);
is null because you didn't set contentView.
Probably the reason for this is: you're setting OnClickListener to null in AlertDialog.Builder in setNeutralButton("OK", null).
So when you click "OK" Android invokes (internally) something like this:
neutralButtonListener.onClick(...);
And the neutralButtonListener is null. So you should just provide empty listener at least.
Post a Comment for "Listview Yielding Nullpointerexception On Setonitemclicklistener"