2011-06-26 40 views
7
 
public class MainActivity extends Activity 
{ 
    private EditText editText; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     editText = (EditText) findViewById(R.id.etext); 
     if(editText == null) 
     { 
      Log.v("editText", "booohooo"); 
     } 
     else 
     { 
      Log.v("editText", "Success"); 
     } 

     final Button button = (Button) findViewById(R.id.gobutton); 

     button.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) 
      { 
       if(editText != null) 
       { 
        Log.v("editText", "is not NULL"); 
       } 
       else 
       { 
        Log.v("editText", "is NULL :("); 
       } 

       // Perform action on click 
       if(editText != null) 
       { 
        editText.getText(); 
       } 
       else 
       { 
        Log.v("editText", "is NULL"); 
       } 
       Log.v("url", editText.getText().toString().trim()); 
       Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(editText.getText().toString().trim())); 
       startActivity(browserIntent); 
      } 
     }); 
    } 
} 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView android_id="@+id/websiteurlheading" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter web site URL" 
    /> 
<EditText android_id="@+id/etext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/websiteurlheading" 
    /> 
<Button android:id="@+id/gobutton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enter" 
    /> 
</LinearLayout> 

任何幫助返回null表示讚賞。findViewById的EditText上

+0

這個XML文件的名稱是什麼? – Haphazard

回答

10

確保setContentView(R.layout.main);設置爲正確的佈局。如果你已經做了一個新的(包括上面的xml代碼),然後使用它來設置內容視圖 - setContentView(R.layout.your_xml_filename);

+0

我剛剛修改main.xml,因此上面的代碼應該已經工作。此外獲取按鈕正在工作。 – jayesh

+0

發現問題..typo android_id應該是android:id – jayesh

+0

我有同樣的問題,這是原因。謝謝 – xzdead

0

我剛剛幾秒鐘前面臨這個問題,我搜索了幾個Stackoverflow的帖子。儘管我自己找到了解決方案。

只要確保您沒有在onCreate方法中調用editText.getText().toString();。因爲它只會返回預加載的值。因此,在這種情況下,editext將始終返回null。

1

變化THES在你的瀏覽

android_id="@+id/websiteurlheading" 

android_id="@+id/etext" 

和改變視圖ID般的線條此

android:id="@+id/websiteurlheading" 
android:id="@+id/etext" 

<TextView android:id="@+id/websiteurlheading" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter web site URL" 
    /> 
<EditText android:id="@+id/etext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/websiteurlheading" 
    /> 
0

如果使用的setContentView佈局會發生這種情況功能不是與您的EditBox放置佈局相同。