Implementing buttons in GridView
<p>I'm trying to implement a GridView with button. The program shows no errors but the application force closes when I press on a button.Here's my code.</p>

<p>Main Code</p>

<pre><code>package android.work;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.Toast;

public class App2Activity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView grid1=(GridView)findViewById(R.id.gridView1);
        grid1.setAdapter(new ButtonAdapter(this));
    }
    public void func(int position)
    {
        Toast.makeText(this, position, Toast.LENGTH_SHORT).show();
    }
}
</code></pre>

<p>Code for Installing buttons in Gridview
    package android.work;</p>

<pre><code>import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

public class ButtonAdapter extends BaseAdapter 
{
    public Context mcontext;
    public ButtonAdapter(Context c)
    {
        mcontext=c;
    }
    public int getCount() 
    {
        return filesnames.length;
    }

public Object getItem(int arg0) 
{
    return null;
}

public long getItemId(int position) 
{
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) 
{
    Button btn;
    if(convertView==null)
    {
        btn=new Button(mcontext);
        btn.setLayoutParams(new GridView.LayoutParams(100,55));
        btn.setPadding(8, 8, 8, 8);
    }
    else
    {
        btn=(Button)convertView;
    }
    btn.setText(filesnames[position]);
    btn.setTextColor(Color.WHITE);
    //btn.setBackgroundResource(R.drawable.button_onoff_indicator_off);
    btn.setId(position);
    btn.setOnClickListener(new myonclicklistener(position));
    return btn;
}

public String[] filesnames = {  
        "File 1",  
        "File 2",  
        "Roflcopters"  
        };

}
</code></pre>

<p>OnClickListener Code</p>

<pre><code>package android.work;

import android.view.View;
import android.view.View.OnClickListener;

public class myonclicklistener extends App2Activity implements OnClickListener
{
    private final int position;
public myonclicklistener(int position)
{
    this.position=position;
}

public void onClick(View v) 
{
    func(position);
}
}


02-08 03:54:14.026: E/AndroidRuntime(1961): FATAL EXCEPTION: main
02-08 03:54:14.026: E/AndroidRuntime(1961): java.lang.NullPointerException
02-08 03:54:14.026: E/AndroidRuntime(1961):     at         android.content.ContextWrapper.getResources(ContextWrapper.java:80)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at     android.widget.Toast.makeText(Toast.java:258)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at android.work.App2Activity.func(App2Activity.java:21)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at android.work.myonclicklistener.onClick(myonclicklistener.java:16)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at android.view.View.performClick(View.java:2485)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at android.view.View$PerformClick.run(View.java:9080)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at android.os.Handler.handleCallback(Handler.java:587)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at android.os.Looper.loop(Looper.java:123)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invoke(Method.java:507)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 03:54:14.026: E/AndroidRuntime(1961):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>
