2

私はJavaとAndroidでのプログラミングに不慣れです...ここに私の質問がありますが、私が提供する情報が十分であるかどうかはわかりません。私がやろうとしているのは、単純な2つのアクティビティアプリを作成することです。メインのアクティビティがあり、ユーザーがボタンをクリックすると、新しいレイアウトを設定する新しいアクティビティが起動します。

私は次の2つのWebサイトを見てきました。

http://developer.android.com/guide/components/fragments.html

http://www.vogella.com/articles/Android/article.html#fragments_tutorial

どちらも非常に便利ですが、実装しようとすると問題が発生しました。

public class MainActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button buttonLoadProfile = (Button) findViewById(R.id.buttonLoadProfile);
    buttonLoadProfile.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent profileIntent = new Intent();
            profileIntent.setClass(getActivity(),LoadProfile.class);
//              setContentView(R.layout.profile_layout);
        }
    });
}

私が得るエラーは、「メソッドgetActivity()は、タイプnew View.OnClickListener(){}に対して未定義です」です。

4

2 に答える 2

2

使用する

    Intent profileIntent = new Intent( MainActivity.this, LoadProfile.class );
    startActivity(profileIntent);

これにより、内部から囲んでいるクラスが解決されます。

于 2012-10-22T02:04:59.907 に答える
1

getActivity()新しいView.OnClickListener()インターフェースにはメソッドが記述されていないため、呼び出すことはできません。代わりに、次のようにします。

Intent profileIntent = new Intent(this, LoadProfile.class);

そして追加:

startActivity(profileIntent);

変更を要約すると:

public void onClick(View v) {
            Intent profileIntent = new Intent(this, LoadProfile.class);
            startActivity(profileIntent);

        }
于 2012-10-22T01:27:43.160 に答える