有一个空错误,我一直试图解决它2天。
Booking.java
package one.two;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Booking extends Activity
{
private DBAdapter db;
private Spinner colourSpinner;
public Cursor myCursor;
public TextView txtArrival;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
db = new DBAdapter(this);
this.setContentView(R.layout.booking);
txtArrival = (TextView) findViewById(R.id.txtArrival); // Member assignment
colourSpinner = (Spinner) findViewById(R.id.myspinner); // Member assignment
db.open();
fillData();
db.close();
colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
private void fillData()
{
myCursor = db.getSpinnerData();
startManagingCursor(myCursor);
String[] from = new String[]{DBAdapter.KEY_ARRIVAL};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,R.layout.booking, myCursor, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// Removed this line, since members is set in onCreate
colourSpinner.setAdapter(adapter);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> arg0, View v,int position, long id)
{
boolean result = Booking.this.myCursor.moveToPosition(position);
if (result) {
String title=Booking.this.myCursor.getString(1);
Booking.this.txtArrival.setText(title);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
};
}
logcat的
08-10 03:45:06.467: ERROR/AndroidRuntime(750): Uncaught handler: thread main exiting due to uncaught exception
08-10 03:45:06.567: ERROR/AndroidRuntime(750): java.lang.NullPointerException
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at one.two.Booking$MyOnItemSelectedListener.onItemSelected(Booking.java:54)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at android.widget.AdapterView.fireOnSelected(AdapterView.java:856)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at android.widget.AdapterView.access$200(AdapterView.java:41)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:827)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at android.os.Handler.handleCallback(Handler.java:587)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at android.os.Handler.dispatchMessage(Handler.java:92)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at android.os.Looper.loop(Looper.java:123)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-10 03:45:06.567: ERROR/AndroidRuntime(750): at dalvik.system.NativeStart.main(Native Method)
谢谢。
答案 0 :(得分:2)
第54行(如果SO保留所有格式正确)为Booking.this.txtArrival.setText(title);
,这意味着txtArrival
可能为空。它本应在你的onCreate中初始化,但它显然没有。如果您查看View#findViewById
的文档,您会看到它返回“如果找到则查看,否则返回null。”
我打赌你的booking.xml布局文件中没有id txtArrival
的视图。
答案 1 :(得分:1)
好好看看你的代码
boolean result = Booking.this.myCursor.moveToPosition(position);
或
String title=Booking.this.myCursor.getString(1)
可能是设置空值。最简单的方法是使用调试器逐步执行代码,这样至少可以看出导致NP的确切语句。
答案 2 :(得分:0)
似乎myCursor
为空。您必须查看DBAdapter
以确定DBAdapter.getSpinnerData()
在什么情况下可以返回null。
答案 3 :(得分:0)
似乎 myCursor为NULL 。
您应该定义一个构造函数,在其中初始化字段,稍后使用。