Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 不允许按钮或活动行为的片段_Android_Gridview_Android Fragments - Fatal编程技术网

Android 不允许按钮或活动行为的片段

Android 不允许按钮或活动行为的片段,android,gridview,android-fragments,Android,Gridview,Android Fragments,我有一个主活动,当应用程序打开时启动。一旦活动启动,它就会从主活动onCreate打开一个GridView片段(同时,主活动和片段共享相同的XML布局) 我遇到的问题是,每当我尝试向按钮添加onClick事件时,除非从我的主要活动中删除打开GridView片段的代码,否则什么都不会发生 注意:我在GridView中使用片段,因为我同时显示了很多图像,所以我设置了片段类来有效地处理它们,而不会影响性能 有什么办法可以解决这个问题吗?提前干杯 主要活动: public class ImageGrid

我有一个主活动,当应用程序打开时启动。一旦活动启动,它就会从主活动onCreate打开一个GridView片段(同时,主活动和片段共享相同的XML布局)

我遇到的问题是,每当我尝试向按钮添加onClick事件时,除非从我的主要活动中删除打开GridView片段的代码,否则什么都不会发生

注意:我在GridView中使用片段,因为我同时显示了很多图像,所以我设置了片段类来有效地处理它们,而不会影响性能

有什么办法可以解决这个问题吗?提前干杯

主要活动:

public class ImageGridActivity extends FragmentActivity {
private static final String TAG = "ImageGridActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.image_grid_fragment);
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();

    //Whenever I remove this code here:
    }
    super.onCreate(savedInstanceState);

    if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
        final FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        ft.add(android.R.id.content, new ImageGridFragment(), TAG);
        ft.commit();

    //To here, it works

    Button B1 = (Button) findViewById(R.id.button1);
    B1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



            Log.w("myApp", "no network");

        }
    });

}
}
}
XML:


我正试图从主布局中设置单击事件,如您所见 在这里我能做些什么来解决我的这个问题

您永远不会与活动布局中的
按钮进行交互,因为您直接将
片段
添加到保存内容的
框架布局
,因此片段将覆盖先前设置的
活动的内容。您可以按如下方式修改活动布局:

R.layout.act\u ImageGridActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/frag_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</RelativeLayout>

由于活动和片段的布局相同,单击侦听器将设置在按钮上(但这将来自活动布局),但添加片段时,该按钮将被片段的布局覆盖,因此您不会获得单击事件。或者,我希望您没有试图从片段布局中设置按钮上的单击侦听器。@Luksprog否,我试图从主布局中设置单击事件,如您在此处所见。我能做些什么来解决这个问题呢?谢谢你的帮助。在GridView的实际片段中定义按钮会更容易吗?@Jack这取决于您计划如何使用片段。如果片段将在活动的生命周期中一直保留在那里,那么您可以在片段的布局中定义它。但是,如果您想在活动级别处理click事件,这将有点困难,因为您不能仅使用
findViewById
来查看事务中刚刚设置的片段(事务将异步发生,在当前主UI事件完成后的某个时间(如onCreate finishing)).好的,谢谢你的时间和帮助,这对我帮助很大。祝你今天愉快:)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/frag_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</RelativeLayout>
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.frag_container, new ImageGridFragment(), TAG);
ft.commit();