简体   繁体   English

java.lang.illegalstateexception在activity类的android片段中找不到方法(view)

[英]java.lang.illegalstateexception could not find a method (view) in the activity class android fragment

in my program, i am using a gridview with some images, i want to show a menu when user tapped on an image in gridview, and then select an action to do from the menu showed. 在我的程序中,我正在使用带有一些图像的gridview,我想在用户点击gridview中的图像时显示一个菜单,然后从显示的菜单中选择要执行的操作。

here is my codes: 这是我的代码:

package Kazemi.Alireza.scada;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.FragmentManager;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


@SuppressLint("NewApi")
public class CitiesTab extends Fragment {

AnimationDrawable[] frameAnimation;
ImageAdapter ia;
GridView gridView;
int in;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return (LinearLayout)inflater.inflate(R.layout.citytab, container, false);
}

public void onStart()
{
    super.onStart();
    ia = new ImageAdapter(getActivity());
    gridView = (GridView) getActivity().findViewById(R.id.gridview);
    gridView.setAdapter(ia);
    gridView.post(new Starter());
    gridView.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            dialog = new Dialog(getActivity(), android.R.style.Theme_InputMethod);
            dialog.setContentView(R.layout.pump_menu);
        }});
    /*Button btn = (Button) getActivity().findViewById(R.id.Button_pumpInfo);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "You clicked on Item 1",
                    Toast.LENGTH_LONG).show();
        }
    });*/
}

public void Btn_pumpInfo_Clicked(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(getActivity(), "You clicked on Item 1",
            Toast.LENGTH_LONG).show();
}

} }

here is my pump_menu.xml code: 这是我的pump_menu.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#a5c5f0"
    android:orientation="vertical" >

<Button 
    android:id="@+id/Button_pumpInfo" 
    android:layout_height="40dp" 
    android:text="@string/menu_pumpinfo_item1"
    android:textSize="11sp" 
    android:layout_width="125dp"
    android:background="#a5c5f0"
    android:onClick="Btn_pumpInfo_Clicked"/>

and citytab.xml : 和citytab.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<GridView 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center" />

<ImageView 
    android:id="@+id/gifViewer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="center"/>

</LinearLayout>

when i use this code, an error occured : 当我使用此代码时,出现错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{Kazemi.Alireza.scada/Kazemi.Alireza.scada.MainMenu}: java.lang.NullPointerException

and when i comment the Btn_pumpInfo_Clicked() method, and uncommented the button listener in onStart() the following error is occured: 当我评论Btn_pumpInfo_Clicked()方法,并在onStart()中取消注释按钮监听器时,发生以下错误:

java.lang.IllegalStateException: Could not find a method Btn_pumpInfo_Clicked(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'Button_pumpInfo'

where is the problem? 问题出在哪儿? Thank You 谢谢

You can't register the onClick callback(using android:onClick ) for the Button (from the layout of the Dialog ) in the Fragment class because Android will not find it as it will search only the Activity for a method matching that name( Btn_pumpInfo_Clicked ) and it will throw that exception. 你不能注册onClick回调(使用android:onClick )的Button (从布局Dialog中) Fragment类,因为Android将找不到它,因为它会搜索Activity匹配该名称的方法( Btn_pumpInfo_Clicked )它将抛出该异常。 Instead look for the Button in the Dialog and assign it a normal listener: 而是在Dialog查找Button并为其分配一个普通的侦听器:

//...
dialog.setContentView(R.layout.pump_menu)
Button b = (Button) dialog.findViewById(R.id.Button_pumpInfo);
b.setOnClickListener(new OnClickListener() {

   @Override
   public void onCLick(View v) {
       // profit
   }
});

Or move the method : 或者移动方法:

public void Btn_pumpInfo_Clicked(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(getActivity(), "You clicked on Item 1",
            Toast.LENGTH_LONG).show();
}

in the Activity class if it fits you. Activity类中,如果它适合你。

If this bug is happening only on Android 2.x check your Activity class hierarchy for Android 4 specific class members: 如果仅在Android 2.x上发生此错误,请检查Android 4特定类成员的Activity类层次结构:

public class HomeActivity extends Activity {

  private android.app.ActionBar.LayoutParams customTitleParams;

  ....
}

The class android.app.ActionBar.LayoutParams is not available on Android 2.x and this can cause the problem. Android 2.x上没有类android.app.ActionBar.LayoutParams ,这可能会导致问题。

This can also happen when the method to call on onClick of the View(Button) isn't accessible or isn't available in the respective activity. 当调用onClick of View(按钮)的方法无法访问或在相应的活动中不可用时,也会发生这种情况。 Steps to check: 检查步骤:

  • Check the method name in the value for android:onClick attribute matches the method actually written. 检查android的值中的方法名称:onClick属性与实际编写的方法匹配。
  • Check the access modifier of the method is public so as to be available to the execute. 检查方法的访问修饰符是否公开,以便可用于执行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:在Android的活动类中找不到方法sms(View) - java.lang.IllegalStateException: Could not find a method sms(View) in the activity class in Android java.lang.IllegalStateException:在活动类中找不到onConvertButtonClick(View)方法 - java.lang.IllegalStateException:Could not find a method onConvertButtonClick(View) in the activity class java.lang.IllegalStateException:在活动类com.test.apps.test.MainActivity的onClick中找不到方法ExitApp(View) - java.lang.IllegalStateException: Could not find a method ExitApp(View) in the activity class com.test.apps.test.MainActivity for onClick Android应用程序:java.lang.IllegalStateException:无法执行活动的方法 - Android app: java.lang.IllegalStateException: Could not execute method of the activity Android:java.lang.IllegalStateException:无法执行活动的方法 - Android: java.lang.IllegalStateException: Could not execute method of the activity Android java.lang.IllegalStateException:无法执行活动的方法 - Android java.lang.IllegalStateException: Could not execute method of the activity java.lang.IllegalStateException:无法执行Android活动的方法 - java.lang.IllegalStateException: Could not execute method of the activity with Android java.lang.IllegalStateException: Could not find method count(View) in a parent or ancestor Context for android:onClick attribute defined on view class - java.lang.IllegalStateException: Could not find method count(View) in a parent or ancestor Context for android:onClick attribute defined on view class java.lang.IllegalStateException:在视图类上定义的 android:onClick 属性的父级或祖先上下文中找不到方法 pay(View) - java.lang.IllegalStateException: Could not find method pay(View) in a parent or ancestor Context for android:onClick attribute defined on view class java.lang.IllegalStateException:找不到方法onClick(View) - java.lang.IllegalStateException: Could not find a method onClick(View)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM