将微调器添加到我的应用

时间:2015-09-19 16:50:17

标签: java android android-studio spinner android-spinner

我的应用有问题。我是初学者。当我将Spinner添加到我的应用程序时,我无法运行它:/。我不知道我该怎么做才能跑步。我在运行时插入片段应用代码和报告。

  

09-19 16:32:09.041 2702-2702 /?我/艺术:不迟到-Xcheck:jni(已经开启)
     09-19 16:32:09.060 2702-2709 /? E / art:向调试器发送回复失败:管道损坏      09-19 16:32:09.060 2702-2709 /?我/艺术:调试器不再有效      09-19 16:32:09.215 2702-2702 /? D / AndroidRuntime:关闭VM      09-19 16:32:09.215 2702-2702 /? E / AndroidRuntime:致命异常:主要          处理:com.example.myapplication,PID:2702           java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myapplication / com.example.myapplication.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法'void   android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'上一个   null对象引用                   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)                   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)                   在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)                   在android.os.Handler.dispatchMessage(Handler.java:102)                   在android.os.Looper.loop(Looper.java:135)                   在android.app.ActivityThread.main(ActivityThread.java:5257)                   at java.lang.reflect.Method.invoke(Native Method)                   在java.lang.reflect.Method.invoke(Method.java:372)                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)            引起:java.lang.NullPointerException:尝试调用虚方法'void   android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'上一个   null对象引用                   在com.example.myapplication.MainActivity.onCreate(MainActivity.java:52)                   在android.app.Activity.performCreate(Activity.java:5990)                   在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)                   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)       在android.app.ActivityThread.access $ 800(ActivityThread.java:151)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)       在android.os.Handler.dispatchMessage(Handler.java:102)       在android.os.Looper.loop(Looper.java:135)       在android.app.ActivityThread.main(ActivityThread.java:5257)       at java.lang.reflect.Method.invoke(Native Method)       在java.lang.reflect.Method.invoke(Method.java:372)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)       09-19 16:33:27.440 2702-2702 /? I /处理:发送信号。 PID:2702 SIG:9

public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    //Spinner
    Spinner spinner;
    ArrayAdapter<CharSequence> adapter;

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

        // Spinner
        spinner = (Spinner)findViewById(R.id.spinner);
        adapter = ArrayAdapter.createFromResource(this,R.array.maps,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();

            }

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

            }
        });

0 个答案:

没有答案