Contextmenu in ListActivity

Hello developers,

I'm loading an object from the database and on each object that is loaded comes a little arrow. On this arrow I am setting a context menu, but this is giving me java nullexception errors. Could someone tell me what's going wrong?

Thanks in advance!

`using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Android.Util;

using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;

using Android.Net;
using Java.IO;
using System.Resources;
using Java.Net;
using Java.Nio;

namespace YanniekAPP
{
[Activity (Label = "Objects", Icon = "@drawable/icon")]
public class ListManagerActivity : ListActivity
{
private ImageButton overflowButton;

    private ImageView klikImage2;

    private TextView textView;
    private List<Object> _objectItems= new List<Object>();
    private MyListAdapter adapter;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        bool requestWindowFeature = false;
        RequestWindowFeature (WindowFeatures.ActionBar);

        //SetContentView (Resource.Layout.list_item_row);

        adapter = new MyListAdapter(this);
        ListView.Adapter = adapter;
        ListView.ItemClick += OnListItemClick;

        ActionBar.NavigationMode = ActionBarNavigationMode.Standard;
        ActionBar.SetTitle(Resource.String.actionbar_title);
        ActionBar.SetDisplayShowTitleEnabled (true);
        ActionBar.SetDisplayHomeAsUpEnabled(true);

        klikImage2 = FindViewById<ImageView> (Resource.Id.image);
        RegisterForContextMenu (klikImage2);
        this.klikImage2.Click += new EventHandler (OnImageViewClick);

    }

    /*
     * attach the menu to the menu button of the device
     * for this activity
     */
    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        base.OnCreateOptionsMenu (menu);

        menu.Add ("test");
        menu.Add ("test2");
        menu.Add ("test3");
        menu.Add ("test4");
        menu.Add ("test5");

        MenuInflater inflater = this.MenuInflater;

        inflater.Inflate (Resource.Menu.mainmenu, menu);

        return true;
    }

    protected void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        var listView = sender as ListView;
        Object objectitem = adapter.GetObjectItems(e.Position);

        Intent intent = new Intent(this,typeof(Detailview));
        intent.PutExtra("id", objectitem.ID);
        intent.PutExtra("name", objectitem.Name);
        intent.PutExtra("detail", objectitem.Description);
        intent.PutExtra("url", objectitem.Url);
        StartActivity(intent);
    }

    /// <param name="item">The menu item that was selected.</param>
    /// <summary>
    /// This hook is called whenever an item in your options menu is selected.
    /// </summary>
    /// <returns>To be added.</returns>
    public override bool OnOptionsItemSelected (IMenuItem item)
    {
        base.OnOptionsItemSelected (item);

        switch (item.ItemId)
        {
        case Resource.Id.add:
            break;

        case Resource.Id.refresh:
            {
                Finish ();
                StartActivity (typeof(ListManagerActivity));
                break;
            }
        default:
            break;
        }

        return true;
    }

    public override void OnCreateContextMenu (IContextMenu menu, View v, IContextMenuContextMenuInfo menuInfo)
    {
        base.OnCreateContextMenu (menu, v, menuInfo);

        MenuInflater menuInflater = new MenuInflater (this);
        menuInflater.Inflate (Resource.Menu.contextmenu, menu);
    }


    public void OnImageViewClick (object sender, EventArgs args)
    {
        ((ImageView)sender).ShowContextMenu ();
    }


    public override bool OnContextItemSelected (IMenuItem item)
    {
        base.OnContextItemSelected (item);

        switch (item.ItemId)
        {
        case Resource.Id.add:
            {
                this.textView.Text = "Add option selected";
                break;
            }
        case Resource.Id.refresh:
            {
                this.textView.Text = "Refresh option selected";
                break;
            }
        default:
            break;
        }

        return true;
    }
}

} `

and the layout:

`<?xml version="1.0" encoding="utf-8"?>



</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_margin="5dp"
    android:orientation="vertical"
    android:layout_weight="2">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt_row_name"
        android:textColor="#000" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt_row_note"
        android:textColor="#000" />
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:orientation="vertical"
    android:layout_weight="4"
    android:layout_alignParentRight="true"
    android:layout_marginTop="4dp">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt_id"
        android:layout_alignParentRight="true"
        android:textColor="#000" />
   <ImageView

    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/image"
    android:src="@drawable/arrow" 
    android:layout_alignParentRight="true"
    android:layout_marginTop="25dp" />



</LinearLayout>


`

Posts

Sign In or Register to comment.