来自 button.setOnClickListener() 的 NullPointerException

NullPointerException from button.setOnClickListener()

我得到这个 NullPointerException 没有明确的理由。

我的相关 java:

Button startAddingDatesButton;
java.util.Date dateInserted;
Calendar calendarDateInserted;
Calendar calendarDateInserted2;
String finalDateShown = "";
List<MyItem> myItemList = new ArrayList<SexAcivity.MyItem>();
MyArrayAdapter myArrayAdapter;
public interface ButtonClickListener 
{
    public abstract void onButtonClick(int position);
}
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sex_acivity);


    startAddingDatesButton = (Button) findViewById(R.id.add_first_row_button_id);

    dateInserted = new java.util.Date();
    calendarDateInserted = Calendar.getInstance();
    calendarDateInserted2 = Calendar.getInstance();

    myArrayAdapter = new MyArrayAdapter(SexAcivity.this,R.layout.list_view_single_row,myItemList,new ButtonClickListener() 
    {   
        @Override
        public void onButtonClick(int position) 
        {

        }
    });
    listView.setAdapter(myArrayAdapter);

    myArrayAdapter = new MyArrayAdapter(SexAcivity.this,R.layout.list_view_single_row,myItemList,new ButtonClickListener() 
    {   
        @Override
        public void onButtonClick(int position) 
        {

        }
    });
    listView.setAdapter(myArrayAdapter);

    startAddingDatesButton.setOnClickListener(new View.OnClickListener()  // Start adding dates button click
    {
        @Override
        public void onClick(View v)
        {
            startAddingDatesButton.setVisibility(Button.INVISIBLE);
            if (firstInsertionFlag) // First date insertion
            {
                dateInserted = getDateFromDatePicker(datePicker);
                calendarDateInserted.setTime(dateInserted);
                finalDateShown = getStringRepresentationOfDate(calendarDateInserted.get(Calendar.DAY_OF_WEEK)) + " " +  (calendarDateInserted.get(Calendar.MONTH) + 1) +  "/" + (calendarDateInserted.get(Calendar.DATE)) + "/" + (calendarDateInserted.get(Calendar.YEAR));                

                myArrayAdapter.add(new MyItem(finalDateShown));

                firstInsertionFlag = false; 
            }
        }
    });

我的相关 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mylifeinformationsharedsocial.SexAcivity" >
 <Button
    android:id="@+id/add_first_row_button_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/add_first_row_button_text"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    />        

</RelativeLayout>

LogCat:

07-25 13:15:36.737: E/AndroidRuntime(19382): FATAL EXCEPTION: main
07-25 13:15:36.737: E/AndroidRuntime(19382): Process:     com.example.mylifeinformationsharedsocial, PID: 19382
07-25 13:15:36.737: E/AndroidRuntime(19382):     java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'     on a null object reference
07-25 13:15:36.737: E/AndroidRuntime(19382):    at  com.example.mylifeinformationsharedsocial.SexAcivity$MyArrayAdapter.getView(SexAcivity.java:284)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.widget.AbsListView.obtainView(AbsListView.java:2387)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.widget.ListView.onMeasure(ListView.java:1182)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.view.View.measure(View.java:17473)
 07-25 13:15:36.737: E/AndroidRuntime(19382):   at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.view.View.measure(View.java:17473)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
07-25 13:15:36.737: E/AndroidRuntime(19382):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)

编辑: 刚刚删除了 setOnClickListener() 方法中的所有代码,它运行良好。这意味着方法主体中有某些东西导致了问题。

编辑: 问题出在 myArrayAdapter.add(new MyItem(finalDateShown)); 行。

当我评论它时没有问题。那么那条线有什么问题?

我的项目class:

class MyItem
{
        public String date;
        public boolean isRadioButtonChecked;

        public MyItem(String date)
        {
            this.date = date;
            this.isRadioButtonChecked = false;
        }
    }

剩下的LogCat:

   07-25 13:15:36.737: E/AndroidRuntime(19382):     at    android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameL     ayout.java:124)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.View.measure(View.java:17473)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.View.measure(View.java:17473)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.View.measure(View.java:17473)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.View.measure(View.java:17473)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2622)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.View.measure(View.java:17473)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1192)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1399)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at  android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5845)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.Choreographer.doCallbacks(Choreographer.java:580)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.Choreographer.doFrame(Choreographer.java:550)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.os.Handler.handleCallback(Handler.java:739)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.os.Handler.dispatchMessage(Handler.java:95)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.os.Looper.loop(Looper.java:135)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at android.app.ActivityThread.main(ActivityThread.java:5274)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at java.lang.reflect.Method.invoke(Native Method)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at java.lang.reflect.Method.invoke(Method.java:372)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
   07-25 13:15:36.737: E/AndroidRuntime(19382):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

编辑:

MyArrayAdapter class:

private class MyArrayAdapter extends ArrayAdapter<MyItem> // My custom array adapter class
{
    private int myResourceId = 0;
    private LayoutInflater myLayoutInflater; 
    private RadioButton mySelectedRadioButton;
    private int mSelectedPosition = -1;
    private ButtonClickListener myClickListener = null;

    public MyArrayAdapter(Context context, int myResourceId, List<MyItem> objects,ButtonClickListener myClickListener) 
    {
        super(context, myResourceId, myItemList);
        this.myResourceId = myResourceId;
        myLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.myClickListener = myClickListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        final ViewHolder holder;

        if (view == null)
        {

            view = myLayoutInflater.inflate(myResourceId, parent, false);
            holder = new ViewHolder();

            holder.dateTextView = (TextView) view.findViewById(R.id.dates_id);
            holder.addDateButton = (Button) findViewById(R.id.add_date_button_id);
            holder.addCommentButton = (Button)view.findViewById(R.id.add_comment_button_id);
            holder.selectDateRadioButton = (RadioButton) view.findViewById(R.id.select_date_radio_button_id);

            holder.addDateButton.setOnClickListener(new View.OnClickListener()
            {   
                @Override
                public void onClick(View v) 
                {
                    holder.dateTextView.setText(finalDateShown);
                }
            });

            view.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) view.getTag();
        }


        holder.addDateButton.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                if(position != mSelectedPosition && mySelectedRadioButton != null)
                {
                    mySelectedRadioButton.setChecked(false);
                }

                mSelectedPosition = position;
                mySelectedRadioButton = (RadioButton) v;
            }
        });


        if(mSelectedPosition != position)
        {
            holder.selectDateRadioButton.setChecked(false);
        }
        else
        {
            holder.selectDateRadioButton.setChecked(true);
            if(mySelectedRadioButton != null && holder.selectDateRadioButton != mySelectedRadioButton)
            {
                mySelectedRadioButton = holder.selectDateRadioButton;
            }
        }
        return view;
    } // End of getView() method



    @Override
    public void add(MyItem object) 
    {
        super.add(object);

    }



    private class ViewHolder
    {
        TextView dateTextView;
        Button addDateButton;
        Button addCommentButton;
        RadioButton selectDateRadioButton;
    }

}

我想可能是出现 NullPointerException 因为你让它不可见。 而且你永远不会让它可见,所以空指针出现在那个时候。

如果您慢慢阅读您的自定义 ArrayAdapter,您会发现那一行

holder.addDateButton = (Button) findViewById(R.id.add_date_button_id); 

必须这样

holder.addDateButton = (Button) view.findViewById(R.id.add_date_button_id); 

第一个 returns 为空,当您尝试设置 OnClickListener 时崩溃。