单击微调器

时间:2015-12-17 14:01:45

标签: nullpointerexception spinner

我试图在两个不同的微调器中显示语言和上下文。 以下是代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    context = getContext();
    rootView = inflater.inflate(R.layout.language_fragment,    container,false);
    dbRepo = DBRepo.getInstance(context);
    getDialog().setTitle(Html.fromHtml("<font size='10' color='#9B9B9B'>Configuration Settings</font>"));

    //language spinner
    Spinner languageSpinner = (Spinner)rootView.findViewById(R.id.language_spinner);
    List<String>languagelist;
    languagelist = dbRepo.getLanguages(HomeFragment.projectGUID);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, languagelist);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    languageSpinner.setAdapter(adapter);

    //context spinner
    Spinner contextSpinner = (Spinner)rootView.findViewById(R.id.context_spinner);
    List<String>contextlist;
    contextlist = dbRepo.getContext(HomeFragment.projectGUID);
    ArrayAdapter<String> adapterC = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, contextlist);
    adapterC.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    contextSpinner.setAdapter(adapterC);
    firstTym = true;
    contextSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    languageSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    return rootView;

}

public class MyOnItemSelectedListener implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        String selectedItem = parent.getItemAtPosition(pos).toString();
        if(firstTym)
        {
            //do nothing
        }
        else {
            //check which spinner triggered the listener
            switch (parent.getId()) {
                //language spinner
                case R.id.language_spinner:
                    Toast.makeText(parent.getContext(), "Language you selected is " + selectedItem, Toast.LENGTH_LONG).show();
                    dbRepo.updateDetails(selectedItem,null);
                    break;
                //context spinner
                case R.id.context_spinner:
                    Toast.makeText(parent.getContext(), "Context you selected is " + selectedItem, Toast.LENGTH_LONG).show();
                    dbRepo.updateDetails(null,selectedItem);
                    break;
            }
        }
        firstTym = false;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub
    }
}

这是我有两个微调器context spinner works fine的代码,但是当我点击language spinner时,它正在投掷null pointer exception

这是我的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:gravity="center"
android:padding="30dp"
android:layout_margin="10dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_weight="0.5"
    android:layout_height="wrap_content"

    android:text="Select Language"
    android:id="@+id/lang_txt"
    android:gravity="center"
    android:textSize="@dimen/abc_text_size_medium_material"
    android:layout_margin="20dp"
    android:layout_alignParentStart="true"/>
<Spinner
    android:id="@+id/language_spinner"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginLeft="5dp"

    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="20dp"
    android:layout_toRightOf="@+id/lang_txt"
     />
</RelativeLayout>
<RelativeLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="30dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_weight="0.5"
    android:layout_height="wrap_content"
    android:text="Select Context"
    android:id="@+id/con_txt"
    android:layout_alignParentStart="true"
    android:gravity="center"
    android:textSize="@dimen/abc_text_size_medium_material"
    android:layout_margin="20dp" />

<Spinner
    android:id="@+id/context_spinner"
    android:layout_width="0dp"
    android:layout_weight="0.5"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="5dp"

    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/con_txt"
    android:layout_alignParentRight="true"
     />
</RelativeLayout>

</LinearLayout>

任何人都可以提出任何建议吗,语言微调器有什么问题?

这是堆栈跟踪

12-18 10:58:09.084  22744-22744/com.example.mediaapp.prototypingcanvas    E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mediaapp.prototypingcanvas, PID: 22744
java.lang.NullPointerException
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
        at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
        at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getDropDownView(AppCompatSpinner.java:624)
        at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getView(AppCompatSpinner.java:620)
        at android.support.v7.widget.AppCompatSpinner.compatMeasureContentWidth(AppCompatSpinner.java:547)
        at android.support.v7.widget.AppCompatSpinner.access$500(AppCompatSpinner.java:65)
        at android.support.v7.widget.AppCompatSpinner$DropdownPopup.computeContentWidth(AppCompatSpinner.java:738)
        at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:763)
        at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
        at android.view.View$PerformClick.run(View.java:18675)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5590)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

解决了!!

实际上,我的列表LanguageList中的字符串值为null,并且抛出了异常