片段中的AutoCompleteTextView获取NullPointerException

时间:2013-11-16 12:18:37

标签: java android exception android-fragments nullpointerexception

我尝试在片段中使用AutoCompleteTextView,但我在autoCompleteTextViewCountry.setAdapter(adapter);处获得异常我使用的是示例代码,但是此示例在标准活动中使用它而不是片段,所以我认为问题在于:

我的代码: ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.iMainActivity, android.R.layout.simple_dropdown_item_1line, COUNTRIES);

原始示例代码: ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);

全班:

    public class SettingsActivity extends Activity {
   public static SettingsActivity iSettingsActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    public SettingsActivity(){
     iSettingsActivity=this;
    }

    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_settings, container, false);

            String COUNTRIES[]={"INDIA","ITALY","JAPAN","USA","ICELAND","INDONESIA","UK","IRAN","IRAQ"};
            final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);
            final TextView textViewSelectedCountry=(TextView)rootView.findViewById(R.id.textViewCountry);
            Button btnSelectedCountry=(Button)rootView.findViewById(R.id.buttonSelectCountry);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.iMainActivity,
                    android.R.layout.simple_dropdown_item_1line, COUNTRIES);

            autoCompleteTextViewCountry.setAdapter(adapter);
            autoCompleteTextViewCountry.setThreshold(1);

            btnSelectedCountry.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String country=autoCompleteTextViewCountry.getText().toString();
                    textViewSelectedCountry.setText("Selected Country: "+country);
                }
            });
            return rootView;
        }
    }
}

完整xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context=".SettingsActivity$PlaceholderFragment"
android:background="@color/myBlack"
    >

<TextView
    android:id="@+id/textViewCountry"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="You Selected :"
    android:textColor="@color/white"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_below="@+id/autoCompleteTextView"
    android:layout_alignParentLeft="true" />

<AutoCompleteTextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:layout_below="@+id/autoCompleteTextView1"
    android:layout_marginTop="20dp"
    android:layout_alignParentLeft="true"
    android:ems="10"
    android:hint="Choose The Country"
    android:id="@+id/autoCompleteTextView">
    <requestFocus />
</AutoCompleteTextView>

<Button
    android:id="@+id/buttonSelectCountry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="          OK          "`enter code here`
    android:layout_below="@+id/textViewCountry"
    android:layout_alignParentLeft="true" />

</RelativeLayout>

5 个答案:

答案 0 :(得分:3)

更改此行

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView);

您传递了错误的ID,因此它正在提供NullPointerException

答案 1 :(得分:2)

更改此行

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.iMainActivity,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

喜欢这个..

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

删除静态字段..

答案 2 :(得分:1)

在这一行: ArrayAdapter adapter = new ArrayAdapter(MainActivity.iMainActivity,                     android.R.layout.simple_dropdown_item_1line,COUNTRIES);

ArrayAdapter应该是上下文的第一个参数, 片段托管在Activity中时,在这种情况下(MainActivity) 上下文应该是 - getActivity()

该片段不应具有其父托管活动的特定知识。

更改它,运行它应该可以工作:)

答案 3 :(得分:0)

使用错误ID autoCompleteTextView1 进行充气的自动完成文本视图。

试试这个:

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView);

而不是

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);

同时更改以下声明

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.iMainActivity,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

我希望这适合你。

答案 4 :(得分:-1)

您需要更改此代码行:

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1); 

到此:

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView);