Fragment For Image Viewer (android)
I'm trying to create a simple fragment that merely displays an image when clicked. I'm getting numerous errors inlcuding 'the method findViewbyId(int) is undefined for the type Exa
Solution 1:
findViewById is a method in the class Activity. To avoid passing an instance of the entire activity use a WeakRefence (see http://developer.android.com/reference/java/lang/ref/WeakReference.html) for example
private WeakReference<MainActivity> activity;
publicExampleFragment(MainActivity mainActivity) {
activity = newWeakReference<MainActivity>(mainActivity);
}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageViewimageview= (ImageView)activity.findViewById(R.id.imageView1);
return Inflater.inflate(R.layout.iamgeview_main, container, false);
}
Solution 2:
I added the following modifications to your code ;
1) Import Fragment from support library(important if running on early versions of android);
2) Insted of Inflater I used the inflater received as method param;
3) I created a method findViewById that can be used in this fragment;
4) I moved some of the initialisation code in the method onActivityCreated ; Here
you cand find more about that method ;
package com.example.gctest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.ImageView;
publicclassExampleFragmentextendsFragment {
Button button;
ImageView image;
publicExampleFragment() {
}
publicstaticExampleFragmentnewInstance() {
returnnewExampleFragment();
}
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//ImageView imageview = (ImageView)findViewById(R.id.imageView1); this will raise NullPointerException because the parent view has not been created. Is is created with this method;return inflater.inflate(R.layout.iamgeview_main, container, false); //just return the view ;
}
@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub//here you can initialise your variables,listeners,e.t.c ;super.onActivityCreated(savedInstanceState);
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
addListenerOnButton();
}
/**
* You can use this method in order to access the child views of the fragment parent view;
* @paramid
* @return
*/protectedViewfindViewById(int id)
{
returngetView().findViewById(id);
}
publicvoidaddListenerOnButton() {
button = (Button) findViewById(R.id.btnChangeImage);
button.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View arg0) {
image.setImageResource(R.drawable.eagle);
}
});
}
Post a Comment for "Fragment For Image Viewer (android)"