看了很多其他帖子并尝试了几种变化但到目前为止没有运气......
使用以下自定义列表适配器获取NPE:
public ListView tripList;
....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
// build a listView adapter
Log.i(LOGTAG,"About to create listView...");
tripList = (ListView) findViewById(android.R.id.list);
Log.i(LOGTAG,"ListView created...");
Log.i(LOGTAG,"About to create adapter...");
tripAdapter = new tripListAdapter(this, tripCur);
setListAdapter(tripAdapter);
}
setContentView(R.layout.activity_trip_display);
...和适配器有这个:
// custom list adaptor class
private class tripListAdapter extends CursorAdapter {
//private final Cursor dataC;
private final LayoutInflater vi;
public tripListAdapter(Context con, Cursor c) {
// super constructor thingy
super(con, c);
//dataC = c;
vi = LayoutInflater.from(con);
}
@Override
public View getView (int position, View tripView, ViewGroup parent){
Log.i(LOGTAG,"In getView...");
// Create a message handling object as an anonymous class.
//mTripClickHandler = new OnItemClickListener() {
// NPE ERROR ON THIS NEXT LINE (line 228):
tripList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do something in response to the click
// Enable other buttons
togBtns(true);
logcat的:
06-13 09:47:49.195: I/WIBBLE(864): ListView created...
06-13 09:47:49.195: I/WIBBLE(864): About to create adapter...
06-13 09:47:49.285: I/WIBBLE(864): OnResume with this many rows returned by cursor: 3
06-13 09:47:49.285: I/WIBBLE(864): In the loop, so cursor is not null....
06-13 09:47:49.375: I/WIBBLE(864): In getView...
06-13 09:47:49.387: D/AndroidRuntime(864): Shutting down VM
06-13 09:47:49.387: W/dalvikvm(864): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
06-13 09:47:49.505: E/AndroidRuntime(864): FATAL EXCEPTION: main
06-13 09:47:49.505: E/AndroidRuntime(864): java.lang.NullPointerException
06-13 09:47:49.505: E/AndroidRuntime(864): at com.example.myfirstapp.DisplayTripsActivity$tripListAdapter.getView(DisplayTripsActivity.java:228)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.widget.ListView.onMeasure(ListView.java:1159)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.view.View.measure(View.java:15848)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
感谢任何建议。
编辑:如果我更换:
//tripList = (ListView) findViewById(android.R.id.list);
使用:
tripList = getListView();
...我继续前进(参见WIBBLE消息)但获得另一个NPE,虽然似乎在某些系统例程中:
06-13 10:26:49.825: I/WIBBLE(934): About to create listView...
06-13 10:26:49.865: I/WIBBLE(934): ListView created...
06-13 10:26:49.865: I/WIBBLE(934): About to create adapter...
06-13 10:26:49.945: I/WIBBLE(934): OnResume with this many rows returned by cursor: 3
06-13 10:26:49.945: I/WIBBLE(934): In the loop, so cursor is not null....
06-13 10:26:50.035: I/WIBBLE(934): In getView...
06-13 10:26:50.035: I/WIBBLE(934): Leaving getView...
06-13 10:26:50.035: D/AndroidRuntime(934): Shutting down VM
06-13 10:26:50.035: W/dalvikvm(934): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
06-13 10:26:50.185: E/AndroidRuntime(934): FATAL EXCEPTION: main
06-13 10:26:50.185: E/AndroidRuntime(934): java.lang.NullPointerException
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.AbsListView.obtainView(AbsListView.java:2179)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.ListView.onMeasure(ListView.java:1159)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.view.View.measure(View.java:15848)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.view.View.measure(View.java:15848)
......以及许多其他行,没有引用我的代码。
答案 0 :(得分:1)
您不应该从onItemClickListener
设置Adapter
。 Adapter
仅关注创建单个列表项视图。在视图充气后,从onItemClickListener
(或Activity
)设置Fragment
。
看起来您当前没有设置您的活动视图,这就是tripList ListView为NULL的原因。使用setContentView(R.layout.your_layout)
将视图设置为xml布局。
您可能希望发布大部分代码。这是活动还是碎片?您是否扩展了基本活动/片段,或其他类似ListActivity / ListFragment。我想知道因为你正在访问android的R文件而不是你自己的(android.R.id.list vs R.id.list)
<强>更新强>
好的,我已经回答了我自己的问题。您正在使用ListActivity(或ListFragment),这就是您使用android的R文件的原因。我会将其更改为getListView()
而不是findViewById
。我仍然会移动setOnItemClickListener
并发布新的NPE例外,因为这是执行此操作的正确方法。
答案 1 :(得分:0)
将tripView变量分配给ListView时,使用:
tripList = (ListView) findViewById(android.R.id.list);
尝试将此行更改为:
tripList = (ListView) findViewById(R.id.list);
不是在android.R
中搜索正确的项目(它可能不会退出),而是在构建项目时生成的R文件中查找ListView项目。
确保在运行之前重建项目。