我的自定义适配器的旋转器SetAdapter返回null指针异常



我正在尝试使用自定义适配器实现旋转器。但是,SetAdapter正在返回NPE。我尝试做各种各样的事情,包括在堆栈溢出中进行此类讨论帖子。请不要给我一个类似答案的链接。我当然已经经历了其中的每一个,对我的代码进行了更改。

这是我的起步性:

public class StartActivity extends Activity{
    int selected;
    String[] strings = {"CoderzHeaven","Google",
        "Microsoft", "Apple", "Yahoo","Samsung"};
    String[] subs = {"Heaven of all working codes ","Google sub",
        "Microsoft sub", "Apple sub", "Yahoo sub","Samsung sub"};


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

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Spinner
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<String> adapter = new MyAdapter(getApplicationContext(), R.layout.row, strings);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(spinner_updated);
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }

    public class MyAdapter extends ArrayAdapter<String>{
        public MyAdapter(Context context, int textViewResourceId,   String[] objects) {
            super(context, textViewResourceId, objects);
        }
        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }
        public View getCustomView(int position, View convertView, ViewGroup parent) {
            View row= convertView;
            if(row==null){
                LayoutInflater inflater=getLayoutInflater();
                row = inflater.inflate(R.layout.row, parent, false);
            }

            TextView label=(TextView)row.findViewById(R.id.company);
            label.setText(strings[position]);
            TextView sub=(TextView)row.findViewById(R.id.sub);
            sub.setText(subs[position]);

            return row;
        }
    }
    AdapterView.OnItemSelectedListener spinner_updated = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selected = position;
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    };

    public void park_clicked(View view){
        Intent myIntent = new Intent(StartActivity.this, MainActivity.class);
        myIntent.putExtra("selected", selected); //Optional parameters
        StartActivity.this.startActivity(myIntent);
    }
}

这是我的EventLog:

05-16 13:06:16.373    9030-9030/parkpurdue.parkpurdue E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{parkpurdue.parkpurdue/parkpurdue.parkpurdue.StartActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2107)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
        at android.app.ActivityThread.access$700(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4918)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at parkpurdue.parkpurdue.StartActivity.onCreate(StartActivity.java:44)
        at android.app.Activity.performCreate(Activity.java:5185)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)

您的布局- activity_start,不包含iD = spinner的旋转视图。

相关内容

  • 没有找到相关文章

最新更新