我正在构建一个包含微调器的应用程序。微调器由 ArrayAdapter 填充,该 ArrayAdapter 包含来自设备上数据库的数据。问题是,每当我启动应用程序时,它会立即崩溃,没有显示任何内容。我完全确定这是 Spinner 的问题,因为在我使用它之前一切都很好。
日志猫:
11-30 12:56:23.660: E/AndroidRuntime(3945): FATAL EXCEPTION: main
11-30 12:56:23.660: E/AndroidRuntime(3945): java.lang.NullPointerException
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.View.measure(View.java:8313)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.View.measure(View.java:8313)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.View.measure(View.java:8313)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.View.measure(View.java:8313)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.os.Looper.loop(Looper.java:130)
11-30 12:56:23.660: E/AndroidRuntime(3945): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-30 12:56:23.660: E/AndroidRuntime(3945): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 12:56:23.660: E/AndroidRuntime(3945): at java.lang.reflect.Method.invoke(Method.java:507)
11-30 12:56:23.660: E/AndroidRuntime(3945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-30 12:56:23.660: E/AndroidRuntime(3945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-30 12:56:23.660: E/AndroidRuntime(3945): at dalvik.system.NativeStart.main(Native Method)
我的代码(持有微调器的活动):
public class UserActivity extends Activity{
private Spinner profiles;
private String[] arrayspinner = new String[100];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.profiles);
Button Submit = (Button) findViewById(R.id.SubmitProfile);
final EditText profileName = (EditText) findViewById(R.id.ProfileName);
profileName.setText("");
this.profiles = (Spinner) findViewById(R.id.spinner1);
SetSql sqler = new SetSql(UserActivity.this);
sqler.open();
arrayspinner = sqler.getProfiles();
sqler.close();
if(arrayspinner==null){
arrayspinner[0] = "No Profiles, Please Create One";
}
@SuppressWarnings({ "rawtypes", "unchecked" })
ArrayAdapter array = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayspinner);
profiles.setAdapter(array);
//I am using the post method to make sure that this spinner wont run when the activity is started
profiles.post(new Runnable(){
public void run(){
profiles.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(!arrayspinner[arg2].equals("No Profiles, Please Create One")){
String selected = arrayspinner[arg2];
Intent intent = new Intent(UserActivity.this, DPAActivity.class);
intent.putExtra("username", selected);
startActivity(intent);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
});
//the button listener
Submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = profileName.getText().toString();
if(name.equals("")){
profileName.setHint("Please Enter Some Data Here");
}else{
SetSql sqlobj = new SetSql(UserActivity.this);
//open a DB connection
sqlobj.open();
String[] profiledata2 = new String[100];
profiledata2 = sqlobj.getProfiles();
//setup a boolean to check if the profile name matches with one from the database
boolean isSame = false;
//run the check
if(profiledata2!=null){
for(int x=0; x<profiledata2.length; x++){
if(profiledata2[x].equals(name)){
isSame=true;
}
}
}
//use the isSame variable!
if(isSame){
profileName.setHint("This profile name is already in use!");
//close connection
sqlobj.close();
}else{
//insert profile into DB
sqlobj.createProfile(name);
//close connection
sqlobj.close();
//setup intent and move to the next activity
Intent intent = new Intent(UserActivity.this, DPAActivity.class);
intent.putExtra("username", name);
startActivity(intent);
}
}
}
});
}
}
如您所见,我的代码还可以选择将数据放入数组中,以防它为空,所以那里没有空值(我希望)。
我的 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your profile, Or create a new one!"
android:textSize="25dp" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="New profile:"
android:textSize="20dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/ProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Profile name..." >
<requestFocus />
</EditText>
<Button
android:id="@+id/SubmitProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Create!" />
</LinearLayout>
</LinearLayout>
编辑
所以我确保我的数组不像你们建议的那样为空,仍然会因同样的错误而崩溃。有什么帮助吗?