简体   繁体   中英

ListView onClick Button (Android Xamarin)

My problem currently is I am unable to change activity the error is "System.NullReferenceException: Object reference not set to an instance of an object"

My second problem is the method of making that ListView (is because I have a viewpager linked with it, on my drawerLayout). By doing that, I only have one button ID which eventually added with 2 or more items "Add, Delete, etc etc". So even if I do an onclick event. Both button on the listview will come out the same activity. So I need the items I added to be separated and able to do different function.

My activity and declarations(not full code)

public class HomepageActivity : FragmentActivity
    {
        DrawerLayout mDrawerLayout;
        List<string> mLeftItems1 = new List<string>();
        ArrayAdapter mLeftAdapter1, mLeftAdapter2;
        ListView mListView;

mLeftItems1.Add("Add"); 
mLeftItems1.Add("Delete"); 
mLeftAdapter1 = new ArrayAdapter(this, Resource.Layout.layout1, Resource.Id.button1, mLeftItems1); 
mListView.Adapter = mLeftAdapter1;

button1.Click += delegate
{
StartActivity(typeof(Login));
};

My axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:textColor="@android:color/background_dark"
        android:textSize="35dp"
        android:clickable="true" />
</LinearLayout>

The mListView is not being instantiated, only declared. So when Adapter assignment executes, mListView is null.

Try this:

var mListView = new ListView();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM