Spinner在Activity中工作但不在Fragment中工作

时间:2015-01-20 04:35:20

标签: android android-layout listview android-intent android-fragments

01-20 10:39:59.721: D/AndroidRuntime(2062): Shutting down VM
01-20 10:39:59.721: W/dalvikvm(2062): threadid=1: thread exiting with uncaught exception (group=0x41632d88)
01-20 10:39:59.731: E/AndroidRuntime(2062): FATAL EXCEPTION: main
01-20 10:39:59.731: E/AndroidRuntime(2062): Process: com.example.mysqltest, PID: 2062
01-20 10:39:59.731: E/AndroidRuntime(2062): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysqltest/com.example.jobtracker.ReadComments}: java.lang.NullPointerException
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.os.Looper.loop(Looper.java:212)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.ActivityThread.main(ActivityThread.java:5135)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at java.lang.reflect.Method.invoke(Method.java:515)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at dalvik.system.NativeStart.main(Native Method)
01-20 10:39:59.731: E/AndroidRuntime(2062): Caused by: java.lang.NullPointerException
01-20 10:39:59.731: E/AndroidRuntime(2062):     at com.example.jobtracker.JobqueueFragment.onCreateView(JobqueueFragment.java:72)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.Fragment.performCreateView(Fragment.java:1700)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.Activity.performStart(Activity.java:5240)
01-20 10:39:59.731: E/AndroidRuntime(2062):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
01-20 10:39:59.731: E/AndroidRuntime(2062):     ... 11 more

4 个答案:

答案 0 :(得分:0)

替换此行

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

由此

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

答案 1 :(得分:0)

尝试改变如下:

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

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

    // ONE CHANGE HERE
    ctx = getActivity().getApplicationContext(); 

    mainListView = (ListView) inflate.findViewById(R.id.list);

    //SECOND CHANGE HERE
    Spinner spinner = (Spinner) inflate.findViewById(R.id.spinner_status);

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapters = ArrayAdapter.createFromResource(ctx,
            R.array.spinner_item, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapters.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapters);

    new JobQueue().execute();
    return inflate;
}

答案 2 :(得分:0)

更改您的代码如下:

首先更改您的ctx,如下所示:

 ctx=JobqueueFragment.this.getActivity();

而不是

ctx = inflater.getContext();

您的布局fragment_jobqueue不包含Spinner,因此您无法直接在Fragment类的Spinner中绑定数据。这是你实际上得到nullpointer错误的地方。

答案 3 :(得分:0)

  

使用此代码

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

    ArrayAdapter<CharSequence> adapters = ArrayAdapter.createFromResource(getActivity(),
                    R.array.spinner_item, android.R.layout.simple_spinner_item);
 adapters.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  

现在您的问题将解决。   有任何疑问在评论中提出