嗨,我是 android 的新手,可以帮助我实现单击 ImageView 以打开新活动。我尝试了几个代码,但应用程序在启动时崩溃。这是我的片段代码
片段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>
片段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);
}
});
}
}