Spinner 在调用条目值时抛出 NullPointerException
Spinner throws NullPointerException when calling an entries value
我的 ActionBar
中有一个 Spinner
,当我选择一个并将其转换为 String
时,我想获取 Spinner
中条目的值.每次我启动 Activity
时,它都是 thorwing NullPointerException
。我该如何解决?
我已经阅读了许多指南和 Whosebug 问题,但我找不到任何更简单且接近我的情况的内容。这是代码。
spinner.xml
<Spinner
android:id="@+id/SpinnerTerms"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:entries="@array/pmf"/>
menu_classviewstudents.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".ClassViewStudents">
<item android:id="@+id/ViewStudentsClassesMenu"
android:showAsAction="ifRoom|withText"
android:icon="@drawable/addstudents"
android:menuCategory="container"
android:title="Add"/>
<item android:id="@+id/AddGrades"
android:showAsAction="ifRoom"
android:actionLayout="@layout/spinner"
android:title="Grades"/>
</menu>
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pmf">
<item>Prelim</item>
<item>Midterm</item>
<item>Finals</item>
</string-array>
</resources>
这是我的 onCreate
,我在这里调用 Spinner
并将条目值放入字符串中:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.classes_view);
SpinnerPMF = (Spinner) findViewById(R.id.SpinnerTerms);
spinnervalue = SpinnerPMF.getItemAtPosition(SpinnerPMF.getSelectedItemPosition()).toString();
db = openOrCreateDatabase("ClassManager", MODE_WORLD_WRITEABLE, null);
reciever = getIntent().getStringExtra("sender");
Cursor c = db.rawQuery("SELECT * FROM '" + reciever + "'", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
tableLayout.setHorizontalScrollBarEnabled(true);
RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout3);
ScrollView sv = new ScrollView(this);
sv.setHorizontalScrollBarEnabled(true);
sv.addView(tableLayout);
rl.addView(sv, new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("StudentID")));
textView1.setPadding(20, 20, 20, 20);
textView1.setTextColor(getResources().getColor(R.color.redactionbar));
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView1.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView1);
final TableRow finalTableRow = tableRow;
textView = new TextView(getApplicationContext());
textView.setText(c.getString(c.getColumnIndex("LastName")));
textView.setPadding(20, 20, 20, 20);
textView.setTextColor(getResources().getColor(R.color.redactionbar));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView);
tableLayout.addView(tableRow);
tableRow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast toast = Toast.makeText(getApplicationContext(), spinnervalue, Toast.LENGTH_SHORT);
toast.show();
/*
if(SpinnerValue == "Prelim")
{
Toast toast = Toast.makeText(getApplicationContext(), "Prelim Selected", Toast.LENGTH_SHORT);
toast.show();
}
else if (SpinnerValue == "Midterm")
{
Toast toast = Toast.makeText(getApplicationContext(), "Midterm Selected", Toast.LENGTH_SHORT);
toast.show();
}
else if (SpinnerValue == "Finals")
{
Toast toast = Toast.makeText(getApplicationContext(), "Finals Selected", Toast.LENGTH_SHORT);
toast.show();
}
*/
}
});
c.moveToNext() ;
}
rl.requestLayout();
db.close();
}
这是 logcat 错误:
02-16 14:10:29.784: E/AndroidRuntime(29837): FATAL EXCEPTION: main
02-16 14:10:29.784: E/AndroidRuntime(29837): Process: test.com.classmanagertest, PID: 29837
02-16 14:10:29.784: E/AndroidRuntime(29837): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.com.classmanagertest/test.com.classmanagertest.ClassViewStudents}: java.lang.NullPointerException
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.access0(ActivityThread.java:139)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.os.Handler.dispatchMessage(Handler.java:102)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.os.Looper.loop(Looper.java:149)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.main(ActivityThread.java:5257)
02-16 14:10:29.784: E/AndroidRuntime(29837): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 14:10:29.784: E/AndroidRuntime(29837): at java.lang.reflect.Method.invoke(Method.java:515)
02-16 14:10:29.784: E/AndroidRuntime(29837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-16 14:10:29.784: E/AndroidRuntime(29837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
02-16 14:10:29.784: E/AndroidRuntime(29837): at dalvik.system.NativeStart.main(Native Method)
02-16 14:10:29.784: E/AndroidRuntime(29837): Caused by: java.lang.NullPointerException
02-16 14:10:29.784: E/AndroidRuntime(29837): at test.com.classmanagertest.ClassViewStudents.onCreate(ClassViewStudents.java:43)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.Activity.performCreate(Activity.java:5411)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
02-16 14:10:29.784: E/AndroidRuntime(29837): ... 11 more
首先检查你的微调器它包含value.If它没有值,你必须排列到你的微调器中。
使用这一行
String[] mTestArray = getResources().getStringArray(R.array.pmf);
SpinnerPMF.setAdapter(new ArrayAdapter<String>(context,android.R.layout.sipmle_spinner_item,mTestArray))
之后
SpinnerPMF = (Spinner) findViewById(R.id.SpinnerTerms);
这一行
希望对您有所帮助
I have a Spinner in my ActionBar, I want to get the value of the
entries in the Spinner when I pick one and convert it to String
如果微调器在 ActionBar
内,则使用 onCreateOptionsMenu
设置选定值:
private String SpinnerSelectedValue="";
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_classviewstudents, menu);
MenuItem item = menu.findItem(R.id.AddGrades);
SpinnerPMF = (Spinner)item.getActionView();
SpinnerPMF.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
SpinnerSelectedValue=parentView.getItemAtPosition(position).toString();
}
});
return true;
}
现在使用 SpinnerSelectedValue
在 table 行的 onClick 中获取选定值。
我的 ActionBar
中有一个 Spinner
,当我选择一个并将其转换为 String
时,我想获取 Spinner
中条目的值.每次我启动 Activity
时,它都是 thorwing NullPointerException
。我该如何解决?
我已经阅读了许多指南和 Whosebug 问题,但我找不到任何更简单且接近我的情况的内容。这是代码。
spinner.xml
<Spinner
android:id="@+id/SpinnerTerms"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:entries="@array/pmf"/>
menu_classviewstudents.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".ClassViewStudents">
<item android:id="@+id/ViewStudentsClassesMenu"
android:showAsAction="ifRoom|withText"
android:icon="@drawable/addstudents"
android:menuCategory="container"
android:title="Add"/>
<item android:id="@+id/AddGrades"
android:showAsAction="ifRoom"
android:actionLayout="@layout/spinner"
android:title="Grades"/>
</menu>
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pmf">
<item>Prelim</item>
<item>Midterm</item>
<item>Finals</item>
</string-array>
</resources>
这是我的 onCreate
,我在这里调用 Spinner
并将条目值放入字符串中:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.classes_view);
SpinnerPMF = (Spinner) findViewById(R.id.SpinnerTerms);
spinnervalue = SpinnerPMF.getItemAtPosition(SpinnerPMF.getSelectedItemPosition()).toString();
db = openOrCreateDatabase("ClassManager", MODE_WORLD_WRITEABLE, null);
reciever = getIntent().getStringExtra("sender");
Cursor c = db.rawQuery("SELECT * FROM '" + reciever + "'", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
tableLayout.setHorizontalScrollBarEnabled(true);
RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout3);
ScrollView sv = new ScrollView(this);
sv.setHorizontalScrollBarEnabled(true);
sv.addView(tableLayout);
rl.addView(sv, new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("StudentID")));
textView1.setPadding(20, 20, 20, 20);
textView1.setTextColor(getResources().getColor(R.color.redactionbar));
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView1.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView1);
final TableRow finalTableRow = tableRow;
textView = new TextView(getApplicationContext());
textView.setText(c.getString(c.getColumnIndex("LastName")));
textView.setPadding(20, 20, 20, 20);
textView.setTextColor(getResources().getColor(R.color.redactionbar));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView);
tableLayout.addView(tableRow);
tableRow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast toast = Toast.makeText(getApplicationContext(), spinnervalue, Toast.LENGTH_SHORT);
toast.show();
/*
if(SpinnerValue == "Prelim")
{
Toast toast = Toast.makeText(getApplicationContext(), "Prelim Selected", Toast.LENGTH_SHORT);
toast.show();
}
else if (SpinnerValue == "Midterm")
{
Toast toast = Toast.makeText(getApplicationContext(), "Midterm Selected", Toast.LENGTH_SHORT);
toast.show();
}
else if (SpinnerValue == "Finals")
{
Toast toast = Toast.makeText(getApplicationContext(), "Finals Selected", Toast.LENGTH_SHORT);
toast.show();
}
*/
}
});
c.moveToNext() ;
}
rl.requestLayout();
db.close();
}
这是 logcat 错误:
02-16 14:10:29.784: E/AndroidRuntime(29837): FATAL EXCEPTION: main
02-16 14:10:29.784: E/AndroidRuntime(29837): Process: test.com.classmanagertest, PID: 29837
02-16 14:10:29.784: E/AndroidRuntime(29837): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.com.classmanagertest/test.com.classmanagertest.ClassViewStudents}: java.lang.NullPointerException
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.access0(ActivityThread.java:139)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.os.Handler.dispatchMessage(Handler.java:102)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.os.Looper.loop(Looper.java:149)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.main(ActivityThread.java:5257)
02-16 14:10:29.784: E/AndroidRuntime(29837): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 14:10:29.784: E/AndroidRuntime(29837): at java.lang.reflect.Method.invoke(Method.java:515)
02-16 14:10:29.784: E/AndroidRuntime(29837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-16 14:10:29.784: E/AndroidRuntime(29837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
02-16 14:10:29.784: E/AndroidRuntime(29837): at dalvik.system.NativeStart.main(Native Method)
02-16 14:10:29.784: E/AndroidRuntime(29837): Caused by: java.lang.NullPointerException
02-16 14:10:29.784: E/AndroidRuntime(29837): at test.com.classmanagertest.ClassViewStudents.onCreate(ClassViewStudents.java:43)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.Activity.performCreate(Activity.java:5411)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-16 14:10:29.784: E/AndroidRuntime(29837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
02-16 14:10:29.784: E/AndroidRuntime(29837): ... 11 more
首先检查你的微调器它包含value.If它没有值,你必须排列到你的微调器中。
使用这一行
String[] mTestArray = getResources().getStringArray(R.array.pmf);
SpinnerPMF.setAdapter(new ArrayAdapter<String>(context,android.R.layout.sipmle_spinner_item,mTestArray))
之后
SpinnerPMF = (Spinner) findViewById(R.id.SpinnerTerms);
这一行
希望对您有所帮助
I have a Spinner in my ActionBar, I want to get the value of the entries in the Spinner when I pick one and convert it to String
如果微调器在 ActionBar
内,则使用 onCreateOptionsMenu
设置选定值:
private String SpinnerSelectedValue="";
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_classviewstudents, menu);
MenuItem item = menu.findItem(R.id.AddGrades);
SpinnerPMF = (Spinner)item.getActionView();
SpinnerPMF.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
SpinnerSelectedValue=parentView.getItemAtPosition(position).toString();
}
});
return true;
}
现在使用 SpinnerSelectedValue
在 table 行的 onClick 中获取选定值。