我在代码中找到一个NullPointerException
,无法找到它的发生位置。请帮忙。
public class SpinnerActivity extends AppCompatActivity implements
OnItemSelectedListener {
// Spinner element
Spinner spinner, spinner2, spinner3, spinner4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
Button proceed;
// Spinner element
spinner = findViewById(R.id.spinner);
spinner2 = findViewById(R.id.spinner2);
spinner3 = findViewById(R.id.spinner3);
spinner4 = findViewById(R.id.spinner4);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
spinner3.setOnItemSelectedListener(this);
spinner4.setOnItemSelectedListener(this);
proceed = (Button) findViewById(R.id.proceed);
proceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(SpinnerActivity.this, Screenlayout.class));
}
});
DatabaseHelper2 helper = new DatabaseHelper2(this);
MovieObject a = new MovieObject();
helper.insertMovie(a);
// Loading spinner data from database
loadSpinnerData();
}
/**
* Function to load the spinner data from SQLite database
* */
private void loadSpinnerData() {
// database handler
DatabaseHelper2 db = new DatabaseHelper2(getApplicationContext());
List<String> dates = db.getAllDates();
List<String> names = db.getAllNames();
List<String> times = db.getAllTimes();
List<String> seats = db.getAllSeats();
ArrayAdapter<String> nameAdapter = setAdapter(names);
ArrayAdapter<String> dateAdapter = setAdapter(dates);
ArrayAdapter<String> timeAdapter = setAdapter(times);
ArrayAdapter<String> seatAdapter = setAdapter(seats);
// Drop down layout style - list view with radio button
nameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
seatAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(nameAdapter);
spinner2.setAdapter(dateAdapter);
spinner3.setAdapter(timeAdapter);
spinner4.setAdapter(seatAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { }
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public ArrayAdapter<String> setAdapter(List<String> strList){
return new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, strList);
}
错误:
Process: com.trivendra.gettickets, PID: 28164
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:457)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:419)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:206)
at android.widget.Spinner.onMeasure(Spinner.java:614)
at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:420)
at android.view.View.measure(View.java:24510)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1210)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1550)
at android.view.View.measure(View.java:24510)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
at android.view.View.measure(View.java:24510)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
at android.view.View.measure(View.java:24510)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:24510)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:24510)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:736)
at android.view.View.measure(View.java:24510)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3004)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1833)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2122)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7595)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
I/Process: Sending signal. PID: 28164 SIG: 9
Application terminated.