2011-06-26 9 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

問題を発見しました。アンドロイドIDはアンドロイド:id – jayesh

+0

でした。同じ問題がありましたが、これが原因でした。ありがとう – xzdead

0

私はこの問題に直面していましたが、私はいくつかのStackoverflow投稿を検索しました。私は自分で解決策を見つけました。

onCreateメソッドでeditText.getText().toString();を呼び出していないことを確認してください。プリロードされた値のみが返されるため、この場合、editextは常にnullを返します。レイアウトはsetContentViewに使用している場合、この問題が発生した

1
あなたのビューで

変更THESライン

android_id="@+id/websiteurlheading" 

android_id="@+id/etext" 

この

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

ようなビューIDを変更機能はありませんEditBox配置レイアウトと同じです。

関連する問題