Android Spinner设置适配器方法显示空指针异常

时间:2016-10-10 14:25:13

标签: java android

我正在尝试创建一个表单。但是在添加微调器时它显示空指针异常。我该怎么办?我该如何解决这个问题? 每次我使用微调器时,它都会在setAdapter()方法中出错 这是我的代码......

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

public class Complaint extends AppCompatActivity implements       AdapterView.OnItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_complaint);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Spinner sp=(Spinner) findViewById(R.id.spinner);
    List<String> categories = new ArrayList<String>();
    categories.add("चौकशी");
    categories.add("तक्रार");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>   (getApplicationContext(), android.R.layout.simple_spinner_item,categories );

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    sp.setAdapter(dataAdapter);

    sp.setOnItemSelectedListener(this);

}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}}

0 个答案:

没有答案