来自 button.setOnClickListener() 的 NullPointerException
Posted
技术标签:
【中文标题】来自 button.setOnClickListener() 的 NullPointerException【英文标题】:NullPointerException from button.setOnClickListener() 【发布时间】:2015-07-25 10:22:15 【问题描述】:我收到这个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_
android:layout_
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_
android:layout_
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));
行。
当我评论它时没有问题。那这条线有什么问题?
MyItem 类:
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 类:
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;
【问题讨论】:
您确定您提供的 XML 文件的名称是activity_sex_acivity
吗?因为您将活动的内容视图设置为 activity_sex_acivity
,所以您需要确保您在此处提供的 XML 名称为 activity_sex_acivity
也添加您的适配器代码...
@Sash_KP 是的。我也会添加我的适配器代码。
@Sachin 我已经编辑了帖子。
在适配器的 getView() 方法中,您尝试将点击侦听器设置为空按钮。
【参考方案1】:
如果你慢慢阅读你的自定义 ArrayAdapter 你会发现那一行
holder.addDateButton = (Button) findViewById(R.id.add_date_button_id);
应该是这样的
holder.addDateButton = (Button) view.findViewById(R.id.add_date_button_id);
第一个返回 null 并且当您尝试设置 OnClickListener 时崩溃。
【讨论】:
是的,就是这样。谢谢你,也感谢所有试图提供帮助的人。欣赏它。【参考方案2】:我想可能是出现 NullPointerException 因为你让它不可见。 而且你永远不会让它可见,所以空指针出现在那个点上。
【讨论】:
我已经尝试删除它,但这不是问题。 @God 你得到了,你的代码哪一行有 NullPointerException? @God 检查您的 Logcat,它会向您显示 cause: 有时还会显示行号,如果您单击它,您会进入该行。你能把你的整个 logcat 发给我们吗? 现在可以很容易地跟踪您实际在哪里得到 NullPointerException。使用 Log 检查该行中所有内容的值。希望它现在能帮你找到确切的问题。 我不太明白 LogCat 中发生了什么。我可以发布所有内容,您可以帮助我吗?以上是关于来自 button.setOnClickListener() 的 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
为啥 WCF 服务能够处理来自不同进程的调用而不是来自线程的调用
来自 viewDidAppear 的 Segue 调用有效,但不是来自 viewWillAppear