java.lang.illegalstateexception找不到在活动课的Andr​​oid片段的方法(视图) [英] java.lang.illegalstateexception could not find a method (view) in the activity class android fragment

查看:232
本文介绍了java.lang.illegalstateexception找不到在活动课的Andr​​oid片段的方法(视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的计划,我使用一个gridview的一些图片,我想说明,当用户点击GridView控件中的图像上的菜单,然后选择一个操作从菜单中做表现。

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.

这是我的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();
}

}

这是我的pump_menu.xml code:

here is my pump_menu.xml code:

<?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"/>

和citytab.xml:

and 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>

当我使用code,发生错误:

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

当我评论的Btn_pumpInfo_Clicked()方法,并取消注释按钮听者ONSTART()下面的错误发生:

and when i comment the Btn_pumpInfo_Clicked() method, and uncommented the button listener in onStart() the following error is occured:

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

推荐答案

您不能注册的onClick 回调(使用安卓的onClick )的按钮(从布局的对话框)的片段类,因为Android将找不到它,因为它会搜索活动的方法,该名称匹配( Btn_pumpInfo_Clicked ),它会抛出该异常。相反,查找按钮对话框并为它指定一个正常的监听器:

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. Instead look for the Button in the Dialog and assign it a normal listener:

//...
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.

这篇关于java.lang.illegalstateexception找不到在活动课的Andr​​oid片段的方法(视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆