使用Android API18和片段布局的NullPointerException

时间:2013-11-06 23:03:03

标签: android android-fragments android-spinner

我使用Android Studio v3.2创建了一个新项目,使用了API 18,它创建了默认应用程序。

我在fragment文件中添加了一个微调器(我第一次使用它时,我不确定这是不是问题。)

不,我只是想在飞行中添加一些虚拟物品,但我的应用程序一直在崩溃,不幸的是....已停止消息。

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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    //Just added this!
    String[] movies = new String[]{"one, two, three"};
    ArrayAdapter<String> movieAdapter = new ArrayAdapter<String>(this, R.id.app_start_select_movie_spinner, movies );
    Spinner spinner = (Spinner)findViewById(R.id.app_start_select_movie_spinner);
    spinner.setAdapter(movieAdapter);
}

以下是logcat中的消息:

11-06 23:57:29.174  16743-16743/org.pervasivesystems.nexusmosaic E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pervasivesystems.nexusmosaic/org.pervasivesystems.nexusmosaic.activity_app_start}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at org.pervasivesystems.nexusmosaic.activity_app_start.onCreate(activity_app_start.java:32)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

第32行是:

spinner.setAdapter(movieAdapter);

所以基本上spinner为空......不知怎的,它无法分配

这可能很简单,但我无法弄明白:(

1 个答案:

答案 0 :(得分:2)

您正在为微调器和适配器微调器项使用相同的资源(它需要指向布局资源文件的指针,而不是视图对象)。

ArrayAdapter<String> movieAdapter = new ArrayAdapter<String>(this, R.id.app_start_select_movie_spinner, movies );
Spinner spinner = (Spinner)findViewById(R.id.app_start_select_movie_spinner);

相反,请尝试:

ArrayAdapter<String> movieAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, movies );
Spinner spinner = (Spinner)findViewById(R.id.app_start_select_movie_spinner);

另外,尝试将适配器初始化移动到片段的onCreateView中。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.your_fragment, container, false);

        String[] movies = new String[]{"one, two, three"};
        ArrayAdapter<String> movieAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, movies );
        Spinner spinner = (Spinner)rootView.findViewById(R.id.app_start_select_movie_spinner);
        spinner.setAdapter(movieAdapter);
        return rootView;
}

要从活动中访问片段,请查看fragment google dev docs

从本质上讲,您可以使用以下代码从您的活动中获取片段的根视图,但它可能有点受欢迎:

YourFragment yourFragment = (YourFragment) getFragmentManager().findViewById(R.id.your_fragment);
if(yourFragment != null)
{
    View fragmentRootView = (View) yourFragment.getView();
    //do your manipulation here.
}