2016-11-26 1 views
0

アクションバーにドロップダウンのAutoCompleteTextViewがあります。AutoCompleteTextView.getText()。toString()がnullです

AutoCompleteTextViewからテキストを取得して文字列に格納しようとしています。

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.location2);//Location2 is my layout 

    android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(false); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setDisplayShowCustomEnabled(true); 
    LayoutInflater inflater = (LayoutInflater) this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View view = inflater.inflate(R.layout.location, null);//location is my custom action bar 
    actionBar.setCustomView(view); 
    Global global = new Global(); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_dropdown_item, global.getUniversities()); 
    AutoCompleteTextView textView = (AutoCompleteTextView) view 
      .findViewById(R.id.searchLocation); 
    textView.setAdapter(adapter); 
... 
} 

public void changeLocation(View view){ 

    View v = View.inflate(this, R.layout.location, null); 
    AutoCompleteTextView searchLocation = (AutoCompleteTextView) 
      v.findViewById(R.id.searchLocation); 
    String searchLocationText = searchLocation.getText().toString(); 
    Toast.makeText(this, searchLocationText, 
      Toast.LENGTH_LONG).show(); 
    ... 
} 

<AutoCompleteTextView 
    android:id="@+id/searchLocation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:drawableLeft="@drawable/search" 
    android:paddingTop="15dp" 
    android:imeOptions="actionSearch" 
    android:inputType="textAutoComplete|textAutoCorrect" 
    android:textColor="#FFFFFF" > 

    <requestFocus /> 
</AutoCompleteTextView> 

オートコンプリート機能は、魔法のように動作...しかし、トーストは、文字列searchLocationTextが空であることを示しています...について間違っている何

アクションバーのオートコンプリート・テキスト・ビュー?

テキストを文字列に変換するにはどうすればよいですか?

編集: - 詳細

私はAutoCompleteTextViewに入力することです、それはドロップダウンリストを自動的に補完し、すべてが正常に動作しています。しかし、私は、文字列の中に、その情報を有効にしようと文字列が空です...

エラー:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.AutoCompleteTextView.getText()' on a null object reference 

エラーが行を参照:

String searchLocationText = searchLocation.getText().toString(); 
+0

であなたは 'changeLocation()'メソッドでは完全に新しい、異なる 'AutoCompleteTextView'を膨張させ、そしてからテキストを取得しようとしている様活性のビューを見つけますそれ。どのビューが 'onCreate()'にあるのかよく分かりませんが、あなたが膨張させているのではなく、 'changeLocation()'の 'AutoCompleteTextView'を見つけてください。 –

+0

それは意味のトンを作る!それに応じて私のプログラムを編集できるかどうかを見てみましょう。答えがあれば教えてください。 –

+0

あまりにも多くのコードを除外しました。あなたは 'onCreate()'で 'view'をどうやって取得しますか? – tynn

答えて

1

あなたがからテキストを取得する必要がありますレイアウトに添付したビュー。しかし、あなたはここで新しい意見を膨らませています。

public void changeLocation(View view) { 
    View v = View.inflate(this, R.layout.location, null); 
    AutoCompleteTextView searchLocation = (AutoCompleteTextView) 
      v.findViewById(R.id.searchLocation); 
    String searchLocationText = searchLocation.getText().toString(); 
    Toast.makeText(this, searchLocationText, 
      Toast.LENGTH_LONG).show(); 
    [...] 
} 

代わりonCreate()

public void changeLocation(View view) { 
    ActionBar actionBar = getSupportActionBar(); 
    view = actionBar.getCustomView(); 
    AutoCompleteTextView searchLocation = (AutoCompleteTextView) view 
      .findViewById(R.id.searchLocation); 
    String searchLocationText = searchLocation.getText().toString(); 
    Toast.makeText(this, searchLocationText, 
      Toast.LENGTH_LONG).show(); 
    [...] 
} 
+0

Uh oh ... Nullオブジェクトリファレンス。私は –

+0

完璧なエラーを投稿します!あなたの時間と忍耐力をありがとう –