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
. It is thorwing NullPointerException
every time I start the Activity
. How do I fix this?
I have read many guides and stackoverflow questions but I can't find anything that is simpler and near my case. Here is the code.
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>
This is my onCreate
where I call my Spinner
and put the entries value in a string:
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();
}
Here is the logcat error:
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.access$800(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