gpt4 book ai didi

android - NullPointerException 错误

转载 作者:行者123 更新时间:2023-11-29 00:50:13 24 4
gpt4 key购买 nike

我的日志

08-06 01:27:34.874: ERROR/AndroidRuntime(721): Uncaught handler: thread main exiting due to uncaught exception
08-06 01:27:34.893: ERROR/AndroidRuntime(721): java.lang.NullPointerException
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at one.two.Booking$MyOnItemSelectedListener.onItemSelected(Booking.java:53)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at android.widget.AdapterView.fireOnSelected(AdapterView.java:856)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at android.widget.AdapterView.access$200(AdapterView.java:41)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:827)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at android.os.Handler.handleCallback(Handler.java:587)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at android.os.Looper.loop(Looper.java:123)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at java.lang.reflect.Method.invoke(Method.java:521)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-06 01:27:34.893: ERROR/AndroidRuntime(721): at dalvik.system.NativeStart.main(Native Method)

预订.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 c;
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);
db.open();
fillData();
db.close();
TextView txtArrival = (TextView) findViewById(R.id.txtArrival);
Spinner colourSpinner = (Spinner) findViewById(R.id.myspinner);
colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}

private void fillData()
{
c = db.getSpinnerData();
startManagingCursor(c);
String[] from = new String[]{DBAdapter.KEY_ARRIVAL};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,R.layout.booking, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Spinner colourSpinner = (Spinner) findViewById(R.id.myspinner);
colourSpinner.setAdapter(adapter);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> arg0, View v,int position, long id)
{
boolean result = Booking.this.c.moveToPosition(position);
if (result) {
String title=Booking.this.c.getString(1);
Booking.this.txtArrival.setText(title);
}

}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub

}
};
}

我想知道如何解决这个错误。谢谢。

最佳答案

您正在创建与您的类成员同名的局部变量。这导致成员被本地成员隐藏,这就是您获得 NullPointerExceptions 的原因。您根本没有为成员提供任何值,这就是它们为空的原因。

你要做的是:

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 c;
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()
{
c = db.getSpinnerData();
startManagingCursor(c);
String[] from = new String[]{DBAdapter.KEY_ARRIVAL};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,R.layout.booking, c, 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.c.moveToPosition(position);
if (result) {
String title=Booking.this.c.getString(1);
Booking.this.txtArrival.setText(title);
}

}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub

}
};
}

关于android - NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3420728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com