1

嗨,我是 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);
        }
    });
}

}

4

7 回答 7

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);
        }
    });
}
于 2013-08-16T10:40:19.723 回答
0

尝试 getApplicationContext() 而不是 getActivity()

于 2013-08-16T10:40:20.530 回答
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);
    }
}
于 2013-08-16T10:41:58.117 回答
0

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

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

fashionImg = (ImageView) getActivity().findViewById(R.id.selectone);
于 2013-08-16T10:42:08.787 回答
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);

}
于 2013-08-16T10:43:20.760 回答
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);
}
于 2013-08-16T10:44:01.153 回答
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;
        }
});
于 2013-08-16T10:57:14.840 回答