ListView yielding nullpointerexception on setOnItemClickListener

后端 未结 4 1290
南笙
南笙 2021-01-25 16:01

I\'m attempting to work with through some tutorial code and add in an OnItemClick Listener, but keep throwing an exception when it hits the listener and crashing my app. This is

相关标签:
4条回答
  • 2021-01-25 16:35

    I am not sure your code will work

    private OnItemClickListener newsSelectListener = new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            AlertDialog.Builder alert=new AlertDialog.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(final int position, View convertView, ViewGroup parent) {
    ......................
    convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //Do Somethings in here
                    }
                }
            });
    
    0 讨论(0)
  • 2021-01-25 16:38

    EDIT

    Ok - and where are you setting setContentView(...)? The lv

    ListView lv = (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.

    0 讨论(0)
  • 2021-01-25 16:49

    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

    <ListView
      android: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

    ListView lv = getListView();
    lv.setOnItemClickListener(newsSelectListener);
    
    0 讨论(0)
  • 2021-01-25 16:56

    I cannot see where you have initialized your layout...so:

    Activity class takes care of creating a window for you in which you can place your UI with setContentView(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:

    <activity android: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.

    0 讨论(0)
提交回复
热议问题