如何在片段中单击ImageView时打开新活动

时间:2013-08-16 10:28:29

标签: android android-layout imageview

嗨,我是Android的新手,有些人可以帮我实现点击我的ImageView打开新活动。我尝试了几个代码,但应用程序在启动时崩溃了。这是我的片段代码

Fragment1.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>

Fragment1.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);
        }
    });
}

}

7 个答案:

答案 0 :(得分:8)

如下所示,将代码添加到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);
        }
    });
}

答案 1 :(得分:0)

尝试使用getApplicationContext()而不是getActivity()

答案 2 :(得分:0)

你需要像这样编写Fragment1类:

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);

     //get the button view
    fashionImg = (ImageView) getView().findViewById(R.id.selectone);
    return rootView;
}

    public void onClick(View v) {
        Intent mainIntent = new Intent(getActivity(), CarouselActivity.class);
        startActivity(mainIntent);
    }
}

答案 3 :(得分:0)

您应该通过以下方式进行更改:

fashionImg = (ImageView) getView().findViewById(R.id.selectone);

fashionImg = (ImageView) getActivity().findViewById(R.id.selectone);

答案 4 :(得分:0)

首先,使用android:onClick="onClick"替换ImageView中的android:onClick="onClickTest"。然后在代码中写下类似的东西:

public void onClickTest(View v){

        //The MainActivity being the starting point
        Intent int = new Intent(MainActivity.this,
                CarouselActivity.class);
        MainActivity.this.startActivity(mainIntent);

}

答案 5 :(得分:0)

在onCreateView中添加侦听器。请参阅片段生命周期,问题是在onCreateView之前调用onCreate,因此到那时你的片段视图不会被添加到父容器视图中。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment1, container, false);
//get the button view
    fashionImg = (ImageView) rootView.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);
        }
    });


    return rootView;

}

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

答案 6 :(得分:0)

尝试以下操作,首先放置像这样的ImageView

 <ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"/>

然后将所有其他ImageView或图像放在此上。 在'Fragment1'类中使用OnTouchListener。

image1 = (ImageView) findViewById(R.id.imageView1);

image1.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

        Intent mainIntent =new Intent(Fragment1.this,CarouselActivity.class);
            stratActivity(mainIntent);
            return true;
        }
});