Reputation: 45
Hi Im a newbie in android, can some help me implement a click on my ImageView to open new activity. I tried several codes but the app crashes on launch. Here is my code on fragments
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);
}
});
}
}
Upvotes: 1
Views: 37417
Reputation: 420
Try the following, first place an ImageView like this
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
Then place all other ImageView or images over this. In your 'Fragment1' class use 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;
}
});
Upvotes: 0
Reputation: 179
add the listener in onCreateView. Refer Fragment lifecycle, the problem is onCreate is called before onCreateView so your fragment's view is not added to the parent container view by that time.
@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);
}
Upvotes: 0
Reputation: 6778
First, replace android:onClick="onClick"
in the ImageView
with something like android:onClick="onClickTest"
. Then in the code write something like:
public void onClickTest(View v){
//The MainActivity being the starting point
Intent int = new Intent(MainActivity.this,
CarouselActivity.class);
MainActivity.this.startActivity(mainIntent);
}
Upvotes: 0
Reputation: 2104
You should change this by:
fashionImg = (ImageView) getView().findViewById(R.id.selectone);
to
fashionImg = (ImageView) getActivity().findViewById(R.id.selectone);
Upvotes: 0
Reputation: 11915
You need to write your Fragment1 class like this :
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);
}
}
Upvotes: 0
Reputation: 6738
As shown below add code into onActivityCreated() instead of 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);
}
});
}
Upvotes: 8