Android Fragment onClick 버튼 방법
내 onClick (View v) XML에서 메서드를 호출하려고하는데 Fragment에서 작동하지 않습니다. 이것은 오류입니다.
01-17 12:38:36.840: E/AndroidRuntime(4171): java.lang.IllegalStateException:
Could not find a method insertIntoDb(View) in the activity class main.MainActivity
for onClick handler on view class android.widget.Button with id 'btn_conferma'
자바 코드 :
public void insertIntoDb(View v) {
...
}
XML :
<Button
android:id="@id/btn_conferma"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0.0dip"
android:layout_height="45dp"
android:layout_marginLeft="2dp"
android:layout_weight="1.0"
android:background="@drawable/bottoni"
android:gravity="center_horizontal|center_vertical"
android:onClick="insertIntoDb"
android:text="SALVA"
android:textColor="#ffffff"
android:textSize="16sp" />
귀하의 활동에는
public void insertIntoDb(View v) {
...
}
Fragment가 아닙니다.
위의 활동을 원하지 않는 경우. 프래그먼트에서 버튼을 초기화하고 리스너를 동일하게 설정하십시오.
<Button
android:id="@+id/btn_conferma" // + missing
그때
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rssitem_detail,
container, false);
Button button = (Button) view.findViewById(R.id.btn_conferma);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// do something
}
});
return view;
}
또 다른 옵션은 조각이 View.OnClickListener를 구현하고 조각 내에서 onClick (View v)을 재정의하도록하는 것입니다. 프래그먼트가 활동과 대화해야하는 경우 원하는 메소드가있는 인터페이스를 추가하고 활동이 인터페이스를 구현하고 해당 메소드를 재정의하도록합니다.
public class FragName extends Fragment implements View.OnClickListener {
public FragmentCommunicator fComm;
public ImageButton res1, res2;
int c;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_test, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
fComm = (FragmentCommunicator) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FragmentCommunicator");
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
res1 = (ImageButton) getActivity().findViewById(R.id.responseButton1);
res1.setOnClickListener(this);
res2 = (ImageButton) getActivity().findViewById(R.id.responseButton2);
res2.setOnClickListener(this);
}
public void onClick(final View v) { //check for what button is pressed
switch (v.getId()) {
case R.id.responseButton1:
c *= fComm.fragmentContactActivity(2);
break;
case R.id.responseButton2:
c *= fComm.fragmentContactActivity(4);
break;
default:
c *= fComm.fragmentContactActivity(100);
break;
}
public interface FragmentCommunicator{
public int fragmentContactActivity(int b);
}
public class MainActivity extends FragmentActivity implements FragName.FragmentCommunicator{
int a = 10;
//variable a is update by fragment. ex. use to change textview or whatever else you'd like.
public int fragmentContactActivity(int b) {
//update info on activity here
a += b;
return a;
}
}
http://developer.android.com/training/basics/firstapp/starting-activity.html http://developer.android.com/training/basics/fragments/communicating.html
This is not an issue, this is a design of Android. See here:
You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
A possible workaround would be to do something like this in your MainActivity:
Fragment someFragment;
...onCreate etc instantiating your fragments
public void myClickMethod(View v){
someFragment.myClickMethod(v);
}
and then in your Fragment class:
public void myClickMethod(View v){
switch(v.getid()){
// Your code here
}
}
The others have already said that methods in onClick are searched in activities, not fragments. Nevertheless, if you really want it, it is possible.
Basically, each view has a tag (probably null). We set the root view's tag to the fragment that inflated that view. Then, it is easy to search the view parents and retrieve the fragment containing the clicked button. Now, we find out the method name and use reflection to call the same method from the retrieved fragment. Easy!
in a class that extends Fragment
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_id, container, false);
OnClickFragments.registerTagFragment(rootView, this); // <========== !!!!!
return rootView;
}
public void onButtonSomething(View v) {
Log.d("~~~","~~~ MyFragment.onButtonSomething");
// whatever
}
all activities are derived from the same ButtonHandlingActivity:
public class PageListActivity extends ButtonHandlingActivity
ButtonHandlingActivity.java:
public class ButtonHandlingActivity extends Activity {
public void onButtonSomething(View v) {
OnClickFragments.invokeFragmentButtonHandlerNoExc(v);
//or, if you want to handle exceptions:
// try {
// OnClickFragments.invokeFragmentButtonHandler(v);
// } catch ...
}
}
It has to define methods for all xml onClick handlers.
com/example/customandroid/OnClickFragments.java:
package com.example.customandroid;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Fragment;
import android.view.View;
public abstract class OnClickFragments {
public static class FragmentHolder {
Fragment fragment;
public FragmentHolder(Fragment fragment) {
this.fragment = fragment;
}
}
public static Fragment getTagFragment(View view) {
for (View v = view; v != null; v = (v.getParent() instanceof View) ? (View)v.getParent() : null) {
Object tag = v.getTag();
if (tag != null && tag instanceof FragmentHolder) {
return ((FragmentHolder)tag).fragment;
}
}
return null;
}
public static String getCallingMethodName(int callsAbove) {
Exception e = new Exception();
e.fillInStackTrace();
String methodName = e.getStackTrace()[callsAbove+1].getMethodName();
return methodName;
}
public static void invokeFragmentButtonHandler(View v, int callsAbove) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
String methodName = getCallingMethodName(callsAbove+1);
Fragment f = OnClickFragments.getTagFragment(v);
Method m = f.getClass().getMethod(methodName, new Class[] { View.class });
m.invoke(f, v);
}
public static void invokeFragmentButtonHandler(View v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
invokeFragmentButtonHandler(v,1);
}
public static void invokeFragmentButtonHandlerNoExc(View v) {
try {
invokeFragmentButtonHandler(v,1);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static void registerTagFragment(View rootView, Fragment fragment) {
rootView.setTag(new FragmentHolder(fragment));
}
}
And the next adventure will be proguard obfuscation...
PS
It is of course up to you to design your application so that the data live in the Model rather than in Activities or Fragments (which are Controllers from the MVC, Model-View-Controller point of view). The View is what you define via xml, plus the custom view classes (if you define them, most people just reuse what already is). A rule of thumb: if some data definitely must survive the screen turn, they belong to Model.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.writeqrcode_main, container, false);
// Inflate the layout for this fragment
txt_name = (TextView) view.findViewById(R.id.name);
txt_usranme = (TextView) view.findViewById(R.id.surname);
txt_number = (TextView) view.findViewById(R.id.number);
txt_province = (TextView) view.findViewById(R.id.province);
txt_write = (EditText) view.findViewById(R.id.editText_write);
txt_show1 = (Button) view.findViewById(R.id.buttonShow1);
txt_show1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Onclick","Onclick");
txt_show1.setVisibility(View.INVISIBLE);
txt_name.setVisibility(View.VISIBLE);
txt_usranme.setVisibility(View.VISIBLE);
txt_number.setVisibility(View.VISIBLE);
txt_province.setVisibility(View.VISIBLE);
}
});
return view;
}
You OK !!!!
참고URL : https://stackoverflow.com/questions/21192386/android-fragment-onclick-button-method
'IT story' 카테고리의 다른 글
git gc fatal을 처리하는 방법 : 잘못된 개체 참조 / 원격 / 원본 / HEAD 오류 : 재팩을 실행하지 못했습니다. (0) | 2020.09.15 |
---|---|
스크래피 스파이더에서 사용자 정의 인수를 전달하는 방법 (0) | 2020.09.15 |
+를 사용하여 람다에 대한 함수 포인터 및 std :: function에 대한 모호한 오버로드 해결 (0) | 2020.09.14 |
CoordinatorLayout이란 무엇입니까? (0) | 2020.09.14 |
프로필이 아닌 페이스 북 페이지의 담벼락에 어떻게 게시합니까? (0) | 2020.09.14 |