用于微调器崩溃的setAdapter函数

时间:2015-12-17 10:47:28

标签: android spinner

我一直在努力学习在Android Studio中使用微调器小部件,但无论我使用什么教程,我总是会在尝试使用setAdapter()函数并在启动时崩溃时获得相同的NullPointerException。我尝试将内容视图切换到setContentView(R.layout.content_main),以查看在这里阅读一些解决方案之后是否会解决它,但它没有用。

MainActivity

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.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String[] country = {"Canada", "Mexico", "USA"};

        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, country);

        Spinner spinner = (Spinner)findViewById(R.id.spinner);

        spinner.setAdapter(stringArrayAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

content_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"/>
</LinearLayout>

activity_main.xml中

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

logcat的

12-17 05:23:53.149 532-532/? E/Zygote: MountEmulatedStorage()
12-17 05:23:53.149 532-532/? E/Zygote: v2
12-17 05:23:53.149 532-532/? I/libpersona: KNOX_SDCARD checking this for 10079
12-17 05:23:53.149 532-532/? I/libpersona: KNOX_SDCARD not a persona
12-17 05:23:53.179 532-532/? I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SAMSUNG-SM-N900A_5.0 ver=27
12-17 05:23:53.179 532-532/? I/SELinux: Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SAMSUNG-SM-N900A_5.0_0027
12-17 05:23:53.179 532-532/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
12-17 05:23:53.179 532-532/? I/art: Late-enabling -Xcheck:jni
12-17 05:23:53.229 532-532/? D/TimaKeyStoreProvider: TimaSignature is unavailable
12-17 05:23:53.229 532-532/? D/ActivityThread: Added TimaKeyStore provider
12-17 05:23:53.279 532-532/com.abbod.christopher.unitconverter D/ResourcesManager: creating new AssetManager and set to /data/app/com.abbod.christopher.unitconverter-1/base.apk
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter D/AndroidRuntime: Shutting down VM
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime: FATAL EXCEPTION: main
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime: Process: com.abbod.christopher.unitconverter, PID: 532
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abbod.christopher.unitconverter/com.abbod.christopher.unitconverter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5944)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:98)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at com.abbod.christopher.unitconverter.MainActivity.onCreate(MainActivity.java:21)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6288)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5944) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
12-17 05:23:53.429 532-532/com.abbod.christopher.unitconverter E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
12-17 05:23:56.349 532-532/com.abbod.christopher.unitconverter I/Process: Sending signal. PID: 532 SIG: 9

2 个答案:

答案 0 :(得分:4)

你有 NPE 因为{/ 1}}

spinner =null

因为您加载了Spinner spinner = (Spinner)findViewById(R.id.spinner); 而您的微调器属于R.layout.activity_main

解决方案:

你应该添加

R.layout.content_main
<include layout="@layout/content_main" />

中的</.....AppBarLayout>之后

答案 1 :(得分:0)

<include layout="@layout/content_main"/>

之后的activity_main.xml文件中添加</android.support.design.widget.AppBarLayout>