2013-05-05 115 views
0

希望我只是傳遞一些愚蠢的東西。EditText getText返回空

問題:我的EditText(2)getText()返回空字符串「」@ runtime in the LISTENER。請參閱onClick(...)

我懷疑它必須與我如何在構建器上create()後對對話框中的searchDialog和setContentView充氣,但無法弄清楚。

CODE:

private void initSearch() { 
    results = new ArrayList<Photo>(); 
    AlertDialog.Builder searchDialog = new AlertDialog.Builder(this); 
    AlertDialog dialog = searchDialog.create(); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    dialog.setContentView(R.layout.search_dialog); 
    searchDialog.setView(inflater.inflate(R.layout.search_dialog, null)); 
    final EditText tagField = (EditText) dialog.findViewById(R.id.tagField); 
    final EditText valueField = (EditText) dialog.findViewById(R.id.valueField); 
    searchDialog.setTitle("Search Photos"); 
    searchDialog.setMessage("Specify tag and value..."); 
    searchDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() { 

     @Override 
    public void onClick(DialogInterface dialog, int which) { 
     try { // PROBLEM: first two lines 
      String tag = tagField.getText().toString(); // TAG WILL BE EMPTY 
    String value = valueField.getText().toString(); // VALUE WILL BE EMPTY 
    String criteria = tag+":\""+value+"\""; 
    ArrayList<String> tags = new ArrayList<String>(); 
      tags.add(criteria); 
    HomeScreen.results = c.getPhotosByTag(tags); 

    if(!tag.equalsIgnoreCase("person") || !tag.equalsIgnoreCase("location")){ 
     throw new IllegalArgumentException("Tag types can only be location or person"); 
    }else if(results.size() == 0) { 
     throw new IllegalArgumentException("No results"); 
    }else { 
     Intent intent = new Intent(HomeScreen.this,SearchResults.class); 
     startActivity(intent); 
      } 
     } catch(Exception e) { 
      dialog.dismiss(); 
    Utilities.createErrorDialog(HomeScreen.this, e.getMessage()); 
     } 

     } 
    }); 
    searchDialog.show(); 
} 

這裏是XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/search_dialog" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/tagText" 
     android:padding="7dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:text="@string/tag" /> 
    <EditText 
     android:id="@+id/tagField" 
     android:padding="7dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text"/> 
    <TextView 
     android:id="@+id/valueText" 
     android:padding="7dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:text="@string/value" /> 
    <EditText 
     android:id="@+id/valueField" 
     android:padding="7dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text"/> 
</LinearLayout> 

任何幫助將是非常讚賞。

+0

你是如何發現'tag'是空的?你在哪裏以及如何輸出它? – 2013-05-05 17:55:26

回答

0

嘗試沿以下線的東西:

private void initSearch() { 
    results = new ArrayList<Photo>(); 
    AlertDialog.Builder searchDialog = new AlertDialog.Builder(this); 
    AlertDialog dialog = searchDialog.create(); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    View v = inflater.inflate(R.layout.search_dialog, null); 
    searchDialog.setView(v); 
    final EditText tagField = (EditText) v.findViewById(R.id.tagField); 
    final EditText valueField = (EditText) v.findViewById(R.id.valueField); 
    searchDialog.setTitle("Search Photos"); 
    searchDialog.setMessage("Specify tag and value..."); 
    searchDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     try { // PROBLEM: first two lines 
       String tag = tagField.getText().toString(); // TAG WILL BE EMPTY 
       String value = valueField.getText().toString(); // VALUE WILL BE EMPTY 
       String criteria = tag+":\""+value+"\""; 
       ArrayList<String> tags = new ArrayList<String>(); 
       tags.add(criteria); 
       HomeScreen.results = c.getPhotosByTag(tags); 

       if(!tag.equalsIgnoreCase("person") || !tag.equalsIgnoreCase("location")){ 
        throw new IllegalArgumentException("Tag types can only be location or person"); 
       }else if(results.size() == 0) { 
        throw new IllegalArgumentException("No results"); 
       }else { 
        Intent intent = new Intent(HomeScreen.this,SearchResults.class); 
        startActivity(intent); 
       } 
     } catch(Exception e) { 
      dialog.dismiss(); 
      Utilities.createErrorDialog(HomeScreen.this, e.getMessage()); 
     } 

    } 
    }); 
    searchDialog.show(); 
} 
+0

Hey Tronic,謝謝你的回答。請檢查onClick方法中的前兩行。我這樣做,但機器人返回是空字符串「」。 – Fer 2013-05-05 17:42:14

+0

@更糟糕的是,我只看到了標題和前幾行。我會稍後更新我的代碼。 – TronicZomB 2013-05-05 17:44:34

+0

@Fer更新了,我也不確定'final'修飾符是否與這個問題有關。但是由於對話框尚未創建,所以視圖不附加到它,所以'findViewById'將返回null。 – TronicZomB 2013-05-05 17:55:14

1

試試這個代碼 - >

LayoutInflater inflater = this.getLayoutInflater(); 
    View view = inflater.inflate(R.layout.search_dialog, null); 
    final EditText tagField = (EditText) view.findViewById(R.id.tagField); 
    final EditText valueField = (EditText) view.findViewById(R.id.valueField); 
    searchDialog.setView(view); 
相關問題