2011-01-24 9 views
0
public class Page1 extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome); 
     final Button button = (Button) findViewById(R.id.welcome); 
      button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
        Intent myIntent = null; 
         myIntent = new Intent(view.getContext(), Page1.class); 
        startActivity(myIntent); 
       } 
      });   
    } 

} 

私はwelcome.xmlという名前の別のXMLファイルから内容をロードしたいのですが、私はエラーにwelcome cannot be resolved or is not a fieldレイアウトは、Androidの[IDやフィールドは解決できない]

このPage1.javaクラスを得るかは、次の画面であります私のAndroidアプリケーションの

マイWelcome.xml

<Button android:text="@+id/Button01" android:id="@+id/welcome" 
android:layout_width="wrap_content" android:layout_height="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
</Button> 

答えて

1

これは機能するはずです。

ハンドラを設定しないと、画面にボタンが表示されますか?

実際にこのファイルの名前は「* W * elcome.xml»ですか?大文字を削除してみてください(名前をwelcome.xmlに変更してください)。その後、クリーン、再構築し、それが今で動作するかどうかチェックしますか...あなたは愚かな間違いを犯している

1

あなたの完全なXMLファイルを貼り付けるとログもらえますか?私の最初の推測では大文字小文字の問題があります、あなたのレイアウトファイルは "ようこそ"と呼ばれ、あなたは "Welcome"にsetContentViewを持っています。また、レイアウトとコントロールの名前が同じではありません。混乱するでしょう。

1

firend:この



public class Page1 extends Activity { 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.welcome); 
    final Button button = (Button) findViewById(**R.id.Button01**);//use id of button here not layout name 

     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = null; 
        myIntent = new Intent(view.getContext(), Page1.class); 
       startActivity(myIntent); 
      } 
     });   
} 

}

1

を見るには、これはすべて何ですかwelcome.xmlには?

あなたのボタンはレイアウトの下にありません。したがって、レイアウトファイル自体が例外をスローします。 次に、android:textが正しくありません。あなたがそこに作られているエントリは、android:id

の下でなければなりません、それはすべきではない:

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

しかし:

final Button button = (Button) findViewById(R.id.Button01); 
0

Welcome.xmlではないのid歓迎とボタンが含まれていますsetContentViewへのレイアウト ビューは、ボタンを追加できるリスト、相対、絶対テーブルなどです。

また、ファイル名と指定されたR.layoutのケースもチェックしてください。 *

リニアレイアウトとボタン付きサンプルxmlファイル。あなたのコード

public class Page1 extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome); 
     final Button button = (Button) findViewById(R.id.ButtonWelcome); 
      button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
        Intent myIntent = null; 

       **//You have called Page1.class again which is the name of this class //again** 
         myIntent = new Intent(view.getContext(), Page1.class); 
        startActivity(myIntent); 
       } 
      });   
    } 

} 

同様に別のアクティビティを作成し、意図でそのクラスが太字にマークコールでwelcome.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:id="@+id/linearlayoutmain" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 

<Button 
android:id="@+id/ButtonWelcome" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/button" 
> 
</Button> 

</LinearLayout> 

としてそれを保存します。

0

ようこそ。XMLは完全ではない、このようなものでなければなりません:あなたはまだproblemasを持っている場合はR.javaが歓迎のIDのような新しいID値で更新されますので、は、R(あなたのプロジェクトをきれいにしようと、また

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:padding="3dip" 
    android:orientation="vertical"> 

    <Button android:text="@+id/Button01" android:id="@+id/welcome" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
    </Button> 

</LinearLayout> 

.id.welcome)R.javaにwelcome idが含まれていないと、そのようなエラーが発生するためです。

関連する問題