Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 微调器setAdapter返回自定义适配器的空指针异常_Java_Android_Android Spinner - Fatal编程技术网

Java 微调器setAdapter返回自定义适配器的空指针异常

Java 微调器setAdapter返回自定义适配器的空指针异常,java,android,android-spinner,Java,Android,Android Spinner,我正在尝试使用自定义适配器实现微调器。但是,setAdapter正在返回NPE。我尝试过做各种各样的事情,包括在StackOverflow中浏览这样的讨论帖子。请不要给我类似答案的链接。当然,我已经检查了它们中的每一个,对代码进行了更改 以下是我的星际触觉: public class StartActivity extends Activity{ int selected; String[] strings = {"CoderzHeaven","Google",

我正在尝试使用自定义适配器实现微调器。但是,setAdapter正在返回NPE。我尝试过做各种各样的事情,包括在StackOverflow中浏览这样的讨论帖子。请不要给我类似答案的链接。当然,我已经检查了它们中的每一个,对代码进行了更改

以下是我的星际触觉:

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);

    }
}

您的布局-
活动\u开始
,不包含ID为
微调器的微调器视图

什么是
微调器更新
?我的意思是
spinner.setonimselectedlistener(spinner\u更新)抛出NPE。我认为您没有初始化
微调器\u updated
interface.Nope。我有。我只是觉得没必要写在这里。它什么也不做,只是返回所选项目的位置。发布完整的活动代码。我已经发布了我的整个活动,并更新了对MyAdapter的回调。请看一看。在代码中标记第44行。@Aditya如果您想继续在此处获得免费帮助,请将问题标记为正确的问题)
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)