NullPointerException:尝试在空对象引用上调用虚拟方法“int java.util.ArrayList.size()”
Posted
技术标签:
【中文标题】NullPointerException:尝试在空对象引用上调用虚拟方法“int java.util.ArrayList.size()”【英文标题】:NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 【发布时间】:2015-01-24 16:36:17 【问题描述】:我是 Fragments
和自定义 ListView
适配器的新手。谁能帮帮我?
我有我的Fragment
,我有我的ListView
public class RecordingListFragment extends Fragment implements OnItemClickListener
ListView mListView;
TextView emptyView;
Button options, delete,edit;
ArrayList<String> recordings = null;
RecordingsListAdapter mAdapter;
public RecordingListFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.recording_list_fragment, container,false);
options = (Button) rootView.findViewById(R.id.options);
edit = (Button) rootView.findViewById(R.id.edit);
delete = (Button) rootView.findViewById(R.id.delete);
mListView = (ListView) rootView.findViewById(R.id.list);
emptyView = (TextView) rootView.findViewById(R.id.empty);
mAdapter = new RecordingsListAdapter(this, recordings);
mListView.setAdapter(mAdapter);
mListView.setEmptyView(emptyView);
mListView.setOnItemClickListener(this);
return rootView;
还有我的 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/recording_list_fragment"
android:layout_
android:layout_
android:background="#ffffff"
android:orientation="vertical">
<RelativeLayout
android:layout_
android:layout_
android:orientation="horizontal"
android:background="@drawable/title" >
<TextView
android1:id="@+id/title"
android1:layout_
android1:layout_
android1:layout_alignParentTop="true"
android1:layout_centerHorizontal="true"
android1:layout_marginStart="80dp"
android1:gravity="center"
android1:text="@string/app_name"
android1:textAlignment="center"
android1:textColor="#000000"
android1:textStyle="bold" />
</RelativeLayout>
<FrameLayout
android:layout_
android:layout_>
<ListView
android:id="@+id/list"
android:animateLayoutChanges="true"
android:layout_
android:layout_/>
<!-- android:divider="@null"-->
<TextView
android:id="@+id/empty"
android:layout_
android:layout_
android:gravity="center"
android:text="@string/empty_list_text"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="180dp" />
</FrameLayout>
</LinearLayout>
我有我的自定义列表项
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal" >
<CheckedTextView
android:id="@+id/textView"
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#000000"
/>
</LinearLayout>
最后,我的Adapter
public class RecordingsListAdapter extends BaseAdapter
Context ctx;
LayoutInflater lInflater;
ArrayList<String> recordings;
RecordingsListAdapter(Context context, ArrayList<String> products)
ctx = context;
recordings = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public RecordingsListAdapter(RecordingListFragment recordingListFragment,
ArrayList<String> recordings)
// TODO Auto-generated constructor stub
@Override
public int getCount()
return recordings.size();
@Override
public Object getItem(int position)
return recordings.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
View view = convertView;
if (view == null)
view = lInflater.inflate(R.layout.custom_list_item, parent, false);
return view;
当我运行它时,我得到了这个空指针
11-26 11:00:45.352: E/AndroidRuntime(10191): java.lang.RuntimeException: Unable to start activity ComponentInfod/recordings.RecordingsActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.os.Handler.dispatchMessage(Handler.java:102)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.os.Looper.loop(Looper.java:135)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.main(ActivityThread.java:5221)
11-26 11:00:45.352: E/AndroidRuntime(10191): at java.lang.reflect.Method.invoke(Native Method)
11-26 11:00:45.352: E/AndroidRuntime(10191): at java.lang.reflect.Method.invoke(Method.java:372)
11-26 11:00:45.352: E/AndroidRuntime(10191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-26 11:00:45.352: E/AndroidRuntime(10191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-26 11:00:45.352: E/AndroidRuntime(10191): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
11-26 11:00:45.352: E/AndroidRuntime(10191): at recordings.RecordingsListAdapter.getCount(RecordingsListAdapter.java:36)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.widget.ListView.setAdapter(ListView.java:487)
11-26 11:00:45.352: E/AndroidRuntime(10191): at recordings.RecordingListFragment.onCreateView(RecordingListFragment.java:47)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.Fragment.performCreateView(Fragment.java:2053)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.BackStackRecord.run(BackStackRecord.java:833)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.Activity.performStart(Activity.java:5948)
11-26 11:00:45.352: E/AndroidRuntime(10191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
11-26 11:00:45.352: E/AndroidRuntime(10191): ... 10 more
提前谢谢你!!!
【问题讨论】:
What is a Null Pointer Exception, and how do I fix it? 的可能重复项 ArrayList您应该初始化您的录音。您正在传递给适配器 null
ArrayList<String> recordings = null; //You are passing this null
【讨论】:
【参考方案2】:这个问题是由于 ArrayList 变量没有被实例化。 需要像下面这样声明“录音”变量,这应该可以解决问题;
ArrayList<String> recordings = new ArrayList<String>();
这会调用默认构造函数并将空字符串分配给录制变量,使其不再为空。
【讨论】:
我像你一样声明变量,但仍然出现错误【参考方案3】:改变
mAdapter = new RecordingsListAdapter(this, recordings);
到
mAdapter = new RecordingsListAdapter(getActivity(), recordings);
同时确保recordings!=null
mAdapter = new RecordingsListAdapter(this, recordings);
【讨论】:
他没有编译时错误,所以这不是问题(他实际上有不同的代码?) 我添加了您的更改 + 我用一些静态数据填充了录音,我收到此错误 `11-26 11:21:47.125: E/AndroidRuntime(11596): java.lang.RuntimeException: Unable to开始活动 ComponentInfoxxxxx/recordings.RecordingsActivity:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean java.util.ArrayList.add(java.lang.Object)” @AdrianAntal 在您的ArrayList
中添加一些记录。 recordings.add("ABC");
recordings.add("AXYZ");
等等……【参考方案4】:
ArrayList 记录 = null; //可以传null
【讨论】:
您好,欢迎来到 Stack Overflow!请拨打tour。感谢您的回答,但您是否还可以添加有关您的代码如何解决问题的解释?查看help center 获取有关如何格式化代码的信息。以上是关于NullPointerException:尝试在空对象引用上调用虚拟方法“int java.util.ArrayList.size()”的主要内容,如果未能解决你的问题,请参考以下文章
NullPointerException 尝试在空对象引用上调用 method.GoogleMap.setMapType(int)'
数据库错误:“NullPointerException:尝试在空对象引用上调用虚拟方法 getApplicationInfo()...”[重复]
原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ActionBar.setNavigationMode(int)”
NullPointerException:尝试在空对象引用上调用虚拟方法 AlertDialog.setTitle(java.lang.CharSequence) [重复]
NullPointerException:尝试在空对象引用上调用虚拟方法“int java.util.ArrayList.size()”