I'm trying to handle 3 ImageButtons on my onClick function. but my application crashes and that's what I get on my logcat :
03-17 21:30:25.372: E/AndroidRuntime(17838): at me.hicham.resume.HomeFragment.onCreate(HomeFragment.java:32)
it points to the line where i've set my ImageButton :
ImageButton fb_button = (ImageButton) getView().findViewById(R.id.button_facebook);
I don't see what might cause this problem (logcat error is not clear enough).
here is my class :
package me.hicham.resume;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import me.hicham.resume.R;
public class HomeFragment extends Fragment implements OnClickListener {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
ImageButton fb_button = (ImageButton) getView().findViewById(R.id.button_facebook);
fb_button.setOnClickListener(this);
ImageButton tw_button = (ImageButton) getView().findViewById(R.id.button_twitter);
tw_button.setOnClickListener(this);
ImageButton ln_button = (ImageButton) getView().findViewById(R.id.button_linkedin);
ln_button.setOnClickListener(this);
return rootView;
}
public void onViewCreated (View view, Bundle savedInstanceState){
/*cool stuff in here*/
}
@Override
public void onClick(View v) {
Intent socialintent;
if (v.getId()== R.id.button_facebook){
socialintent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://"));
this.startActivity(socialintent);
}
else if (v.getId()== R.id.button_twitter){
socialintent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://"));
this.startActivity(socialintent);
}
else if (v.getId()== R.id.button_linkedin){
socialintent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
this.startActivity(socialintent);
}
}
}
Thanks for the help guys :)
You should make all of your findViewById
calls on rootView
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
ImageButton fb_button = (ImageButton) rootView.findViewById(R.id.button_facebook);
fb_button.setOnClickListener(this);
ImageButton tw_button = (ImageButton) rootView.findViewById(R.id.button_twitter);
tw_button.setOnClickListener(this);
ImageButton ln_button = (ImageButton) rootView.findViewById(R.id.button_linkedin);
ln_button.setOnClickListener(this);
return rootView;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.