[Android] android.app.Activity.findViewById(int) on a null object reference
来源:互联网 发布:亦何为而非天下之公乎 编辑:程序博客网 时间:2024/05/16 01:59
问题:
我经常在Android项目中遇到这样一个问题
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
在我的 MainActivity 中我给这个类命名为 FragmentStart
FragmentStart.class
public class FragmentStart extends Fragment { private ListView startList; private String[] stringList; Activity activity = getActivity(); public FragmentStart() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_start, container, false); startList = (ListView) rootView.findViewById(R.id.startList); stringList = getResources().getStringArray(R.array.startList); if (activity != null) { ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, stringList); startList.setAdapter(objAdapter); startList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (startList.getPositionForView(view)) { case 0: // ... break; } } }); } Button add = (Button) activity.findViewById(R.id.add); add.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent myIntent = new Intent(view.getContext(), addItem.class); startActivityForResult(myIntent, 0); } }); return rootView; }}
解决方法
看上去应该是:
Button add = (Button) activity.findViewById(R.id.add);
…应该被改成这样:
Button add = (Button) rootView.findViewById(R.id.add);
原问题
0 0
- [Android] android.app.Activity.findViewById(int) on a null object reference
- android.view.View android.view.View.findViewById(int)' on a null object reference
- 'int android.graphics.Bitmap.getWidth()' on a null object reference
- ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
- Android SearchView android.graphics.drawable..Drawable.setState(int[])' on a null object reference
- findViewById出现错误View.isRootNamespace()' on a null object reference
- Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference
- field 'int android.view.View.mViewFlags' on a null object reference
- Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference
- Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference
- Attempt to read from field 'int android.graphics.Rect.bottom' on a null object reference
- Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
- Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
- Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
- android.support.v7.app.AppCompatActivity.getSupportActionBar()' on a null object reference
- android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference
- android.content.Context.getContentResolver()' on a null object reference错误
- monkey: android viewpager.offset on a null object reference
- LeetCode-344 Reverse String
- eclipse将一段代码抽取为方法Extract Method
- static
- Spring web mvc DispatcherServlet (1)---配置
- 第13期 《与这世界交手》16年5月刊
- [Android] android.app.Activity.findViewById(int) on a null object reference
- 异常典型编程题
- JavaScript---网络编程(4)-Date、Math、Global和自定义对象
- 向下转型
- Python 多进程/多线程 学习笔记
- POJ 2406 Power Strings
- HQL的左连接
- 三组I/O复用函数的比较
- hiho第七周--完全背包