1

**フラグメント 2 のボタンをクリックし、コードに示すようにフラグメント 1 をフラグメント 3 に置き換えたい。
しかし、 findViewById(r.id.mybutton) は 'null' を返しています。
コードのデバッグを試みましたが、作成された 'buttontoggle' を使用でき
ません 上記の問題を解決できることを願っています よろしくお願いします :)

    package com.vivekmishra1991.testfrag;

    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View;
    import android.widget.Button;


    public class MainActivity extends FragmentActivity {

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

    if(findViewById(R.id.fragment_container)!=null){
        if(savedInstanceState!=null){
            return;
        }

               //fragment 1

        f1 F1= new f1();
        F1.setArguments(getIntent().getExtras());

        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, F1).commit();
                   //fragment2(the fragment with the button)

        f2 F2=new f2();
        F2.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container1, F2).addToBackStack(null).commit();
    }



     //code for buttton onclick function
    Button toggleButton = (Button)findViewById(R.id.mybutton);

    toggleButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {  // fragment 3 that has to be replaced with fragment 1
            f3 F3=new f3();
            F3.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,F3).addToBackStack(null).commit();
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

編集

ここに私の activityMain.xml があります

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="match_parent"
 android:layout_width="match_parent"
 android:orientation="vertical"   >

<FrameLayout
         android:id="@+id/fragment_container"
         android:layout_height="0dp"
         android:layout_weight="6"
         android:layout_width="match_parent" />

<FrameLayout
         android:id="@+id/fragment_container1"
         android:layout_height="0dp"
         android:layout_weight="1"
         android:layout_width="match_parent"/>
</LinearLayout>

f2.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">


<Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Next!"
        android:id="@+id/mybutton"
        android:layout_gravity="left|center_vertical"/>
</LinearLayout>
4

1 に答える 1

0

.xml ファイルをチェックして、「mybutton」が存在することを確認できます。

その場合は、[プロジェクト] > [クリーン] を試してください。時折、Eclipse がボタンを R.java ファイルに追加しないことがありますが、プロジェクトをクリーンアップすると、それが修正される傾向があります。そうでない場合は、いつでも xml ファイルでボタンを再作成することができます。

于 2013-07-30T20:21:12.410 に答える