gpt4 book ai didi

android - 单击 fragment 中的 ImageView 时如何打开新 Activity

转载 作者:行者123 更新时间:2023-11-29 14:32:17 28 4
gpt4 key购买 nike

您好,我是 Android 的新手,有人可以帮助我实现点击我的 ImageView 以打开新 Activity 吗?我尝试了几种代码,但应用程序在启动时崩溃了。这是我的 fragment 代码

fragment 1.xml

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif-light"
android:paddingBottom="15dp"
android:paddingTop="8dp" >

<ImageView
android:id="@+id/selectone"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="@drawable/fashion"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:scaleType="centerCrop" />

<ImageView
android:id="@+id/selecttwo"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="@+id/selectone"
android:layout_marginTop="8dp"
android:background="@drawable/lingerie"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/titlelabel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/selectone"
android:background="#80000000"
android:fontFamily="sans-serif-light"
android:padding="10dp"
android:text="Fashion"
android:textColor="#fefefe"
android:textSize="28sp"
android:textStyle="italic" />

<TextView
android:id="@+id/titlelabel2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/selecttwo"
android:background="#80000000"
android:fontFamily="sans-serif-light"
android:padding="10dp"
android:text="Lingerie"
android:textColor="#fefefe"
android:textSize="28sp"
android:textStyle="italic" />
</RelativeLayout>

fragment 1.java

package com.androidbegin.sidemenututorial;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.actionbarsherlock.app.SherlockFragment;

public class Fragment1 extends SherlockFragment {

ImageView fashionImg;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//get the button view
fashionImg = (ImageView) getView().findViewById(R.id.selectone);
//set a onclick listener for when the button gets clicked
fashionImg.setOnClickListener(new OnClickListener() {
//Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(), CarouselActivity.class);
startActivity(mainIntent);
}
});
}

最佳答案

如下所示将代码添加到 onActivityCreated() 而不是 onCreate()

 @Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// get the button view
fashionImg = (ImageView) getView().findViewById(R.id.selectone);
// set a onclick listener for when the button gets clicked
fashionImg.setOnClickListener(new OnClickListener() {
// Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(),
CarouselActivity.class);
startActivity(mainIntent);
}
});
}

关于android - 单击 fragment 中的 ImageView 时如何打开新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18271192/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com