我没有明确的理由给我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));
行。
当我评论它没有问题。那条线路怎么了?
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;
}
}
答案 0 :(得分:2)
如果您慢慢阅读自定义ArrayAdapter,您将找到该行
com.apple.metadata:_kMDItemUserTags
它必须像这样
com.apple.metadata:kMDItemOMUserTags
第一个返回null,当您尝试设置OnClickListener崩溃时。
答案 1 :(得分:0)
我认为可能会出现NullPointerException因为你让它变得无形。 并且你永远不会让它可见,所以在那一点出现空指针。