Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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
Java Android ImageButton可在活动中导航_Java_Android_Imagebutton - Fatal编程技术网

Java Android ImageButton可在活动中导航

Java Android ImageButton可在活动中导航,java,android,imagebutton,Java,Android,Imagebutton,我尝试使用ImageButton作为常规按钮,从一个活动导航到另一个活动。我的活动包含6个图像按钮,我希望用户点击任何图像按钮就可以进入相应的活动。然而,这是行不通的。有人能指导我如何做到这一点吗 这是我的xml文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layou

我尝试使用ImageButton作为常规按钮,从一个活动导航到另一个活动。我的活动包含6个图像按钮,我希望用户点击任何图像按钮就可以进入相应的活动。然而,这是行不通的。有人能指导我如何做到这一点吗

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/book_shelf"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:gravity="center"
        android:text="WELCOME"
        android:textSize="40dip" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="102dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/radioButton2"
        android:layout_alignBottom="@+id/radioButton2"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="23dp"
        android:text="Autoplay" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="33dp"
        android:layout_toRightOf="@+id/radioButton1"
        android:text="Read by self" />

    <ImageButton
        android:id="@+id/ImageButton01"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/radioButton1"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_marginLeft="15dp"
        android:src="@drawable/clown_icon" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_below="@+id/radioButton2"
        android:layout_marginTop="34dp"
        android:layout_toRightOf="@+id/radioButton1"
        android:src="@drawable/nurse_icon" />

    <ImageButton
        android:id="@+id/ImageButton03"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_below="@+id/imageButton1"
        android:layout_marginTop="28dp"
        android:layout_toRightOf="@+id/radioButton1"
        android:src="@drawable/police_w_icon" />

    <ImageButton
        android:id="@+id/ImageButton02"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/ImageButton01"
        android:layout_alignTop="@+id/ImageButton03"
        android:src="@drawable/m_girl_icon" />

    <ImageButton
        android:id="@+id/ImageButton04"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/ImageButton02"
        android:layout_alignTop="@+id/ImageButton05"
        android:src="@drawable/firefighter_icon" />

    <ImageButton
        android:id="@+id/ImageButton05"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/ImageButton03"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="36dp"
        android:src="@drawable/chef_w_icon" />

</RelativeLayout>
如您所见,目前只实现了一个imagebutton。它不起作用。 提前感谢!:-)

请尝试以下代码:

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

         imagebtn1 = (ImageButton) findViewById(R.id.imageButton1);
        imagebtn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent
                        (GirlMenuActivity.this, PracticeActivity.class);
                    startActivity(intent); 
            }
        });
    }


}
p.S:别忘了在清单文件中添加您的
PracticeActivity

重要的是检查你的其他活动。如果在
oncreate
中有什么东西导致运行时错误,那么它也不会工作。

请尝试以下代码:

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

         imagebtn1 = (ImageButton) findViewById(R.id.imageButton1);
        imagebtn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent
                        (GirlMenuActivity.this, PracticeActivity.class);
                    startActivity(intent); 
            }
        });
    }


}
p.S:别忘了在清单文件中添加您的
PracticeActivity


重要的是检查你的其他活动。如果有什么东西导致了oncreate中的运行时错误,那么它也不会工作。

  Intent intent = new Intent(MainActivity.this, second.class);
    startActivity(intent);

请检查您的XML页面图像按钮id并为其指定不同的id,然后输入您的意图代码…并定义您的第二个类,并将其指定给manifest.XML

  Intent intent = new Intent(MainActivity.this, second.class);
    startActivity(intent);

试试这段代码。希望能有所帮助。

imageButton1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent
                    (MainActivity.this, SecondActivity.class);
                startActivity(i); 
        }
    });
}

然后对其他图像按钮使用相同的…

尝试此代码。希望有帮助。

imageButton1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent
                    (MainActivity.this, SecondActivity.class);
                startActivity(i); 
        }
    });
}

然后对另一个图像按钮使用相同的按钮…

是否有任何错误?如果是,请粘贴日志猫。在两个不同的元素上有
android:id=“@+id/imageButton1”
和`android:id=“@+id/imageButton1”`您确定单击的是正确的吗?@Santhosh日志猫中没有错误。我发现了我的错误。这是一个链接器问题。我链接到了错误的活动。谢谢。:-)@Montycarlo这不是关于imageButton的id谢谢你们两位的快速回复。非常感谢。你有什么错误吗?如果是,请粘贴日志猫。在两个不同的元素上有
android:id=“@+id/imageButton1”
和`android:id=“@+id/imageButton1”`您确定单击的是正确的吗?@Santhosh日志猫中没有错误。我发现了我的错误。这是一个链接器问题。我链接到了错误的活动。谢谢。:-)@Montycarlo这不是关于imageButton的id谢谢你们两位的快速回复。非常感谢。