18

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.

Here is my code:

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:

<?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 :

<?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 uncomment the button listener in onStart() the following error occurs:

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?

kometen
  • 6,536
  • 6
  • 41
  • 51
user1793700
  • 219
  • 1
  • 2
  • 9
  • you should post the whole stack trace rather than just one line and do point out the line numbers in your code – nandeesh Dec 06 '12 at 08:56

3 Answers3

43

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.

user
  • 86,916
  • 18
  • 197
  • 190
  • 1
    @user1793700 If my answer helped you could mark it as correct(with the check mark) so the question will become answered. I don't know which of the two options you used, if you're going to use the `Dialog` class then you better make your own dialog extending `Dialog` and in that class set the content view and set the listeners. – user Dec 06 '12 at 11:14
  • I don't know how i can mark as correct! could you explain me how to do it? I used first option in my case, and it works for me:) thanks for your comment :) – user1793700 Dec 06 '12 at 18:23
  • 1
    @user1793700 If it works there is no problem, I wasn't sure when I posted the answer, I waited to see if your code works:) – user Dec 06 '12 at 18:37
  • my line with findViewById(R.id.Button_pumpInfo) compiles but throws a null pointer exception. Any ideas? – fIwJlxSzApHEZIl Feb 19 '13 at 23:49
  • @advocate That is happening because `findViewById` will return null if it can't find the view with the id R.id.Button_pumpInfo. Please start your own question related to the problem providing the proper details like code and stacktrace. – user Feb 20 '13 at 06:44
  • @Luksprog so u mean **android:onClick** attribute not support Fragment layout..!! – CoDe Jan 07 '16 at 05:18
  • @Shubh Yes, the onClick assign method is tied to the Activity. – user Jan 07 '16 at 06:41
3

If this bug is happening only on Android 2.x check your Activity class hierarchy for Android 4 specific class members:

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.

peceps
  • 17,370
  • 11
  • 72
  • 79
  • I am having this same problem, and I think you might have hit it on the head. I thought I might test my app on gingerbread, but, my onClick(View v) is crashing on the class android.support.v7.widget.AppCompatImageButton, cannot find it. – the_gesslar Feb 08 '16 at 22:25
0

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. Steps to check:

  • Check the method name in the value for android:onClick attribute matches the method actually written.
  • Check the access modifier of the method is public so as to be available to the execute.
EffyCoder
  • 48
  • 1
  • 6