尝试使用和API将列表显示到列表视图中

时间:2017-05-08 03:54:50

标签: c# android api xamarin

该页面只是一个列表视图,它所要做的就是将这些项目中的项目返回到要显示的listview。为消息收件箱使用了完全相同的代码,除了返回消息列表之外,还要做同样的事情。但这个似乎不起作用,我的知识有限,因此不理解错误或在哪里看

当我加载页面时,出现此错误:

  

Java.Lang.NullPointerException:尝试调用虚方法   ' java.lang.String java.lang.Object.toString()'在null对象上   参考         at java.lang.NullPointerException:尝试调用虚方法' java.lang.String java.lang.Object.toString()'在null对象上   参考         在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:409)         在android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)         在android.widget.AbsListView.obtainView(AbsListView.java:2363)         在android.widget.ListView.makeAndAddView(ListView.java:1970)         在android.widget.ListView.fillDown(ListView.java:704)         在android.widget.ListView.fillFromTop(ListView.java:765)         在android.widget.ListView.layoutChildren(ListView.java:1744)         在android.widget.AbsListView.onLayout(AbsListView.java:2162)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)         在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)         在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)         在android.widget.FrameLayout.onLayout(FrameLayout.java:261)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)         在android.widget.FrameLayout.onLayout(FrameLayout.java:261)         在com.android.internal.policy.DecorView.onLayout(DecorView.java:726)         在android.view.View.layout(View.java:17637)         在android.view.ViewGroup.layout(ViewGroup.java:5575)         在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)         在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)         在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)         在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6337)         在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:874)         在android.view.Choreographer.doCallbacks(Choreographer.java:686)         在android.view.Choreographer.doFrame(Choreographer.java:621)         在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:860)         在android.os.Handler.handleCallback(Handler.java:751)         在android.os.Handler.dispatchMessage(Handler.java:95)         在android.os.Looper.loop(Looper.java:154)         at android.app.ActivityThread.main(ActivityThread.java:6119)         at at java.lang.reflect.Method.invoke(Native Method)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这是该页面的代码:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Newtonsoft.Json;

namespace Dribl.Droid
{
    [Activity(Label = "Surveys", Theme = "@style/CustomActionBarTheme")]
    public class Surveys : Activity
    {
        LinearLayout surveysBtn;
        LinearLayout availabilityBtn;
        LinearLayout inboxBtn;
        LinearLayout dashboardBtn;

        Button backBtn;

        private List <String> surveys;
        private ListView surveyListview;

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

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Surveys);
            //add the action bar to the layout 
            ActionBar.SetCustomView(Resource.Layout.action_bar);
            ActionBar.SetDisplayShowCustomEnabled(true);

            //action bar nav
            surveysBtn = FindViewById<LinearLayout>(Resource.Id.SurveyLayout);
            surveysBtn.Click += surveyBtn_Click;
            inboxBtn = FindViewById<LinearLayout>(Resource.Id.InboxLayout);
            inboxBtn.Click += InboxBtn_Click;
            availabilityBtn = FindViewById<LinearLayout>(Resource.Id.availabilityLayout);
            availabilityBtn.Click += availabilityBtn_Click;
            dashboardBtn = FindViewById<LinearLayout>(Resource.Id.dashboardLayout);
            dashboardBtn.Click += availabilityBtn_Click;


            WebClient client = new WebClient();
            System.Uri uri = new System.Uri("http://dribl.com/api/getAllMySurveys");
            NameValueCollection parameters = new NameValueCollection();


            parameters.Add("token", GlobalVariables.token);

            client.UploadValuesAsync(uri, parameters);
            client.UploadValuesCompleted += client_UploadValuesCompleted;
        }


        void client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            string json = Encoding.UTF8.GetString(e.Result);
            List<Survey> survey = JsonConvert.DeserializeObject<List<Survey>>(json);

            //display the retrieved msg in the console output
            //Console.WriteLine(message[1].message + " is the message");

            //display the msg in a text view at top of page
            //txt.Text = message[1].message;


            //get the list view create a string to store and add to the list view based on the json return
            surveyListview = FindViewById<ListView>(Resource.Id.surveyListView);
            surveys = new List<string>();

            for (int c = 0; c < survey.Count; c++)
            {
                surveys.Add(survey[c].survey);

            }

            //Msgs.Add(message[1].message);
            //Msgs.Add(message[0].message);

            ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, surveys);

            surveyListview.Adapter = adapter;


        }

        void surveyBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(Surveys));
            this.StartActivity(intent);
            this.Finish();
        }

        void dashboardBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(dashboard));
            this.StartActivity(intent);
            this.Finish();
        }

        void availabilityBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(Availability));
            this.StartActivity(intent);
            this.Finish();
        }

        void InboxBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(MsgInbox));
            this.StartActivity(intent);
            this.Finish();
        }


    }

    public class Survey
    {
        public string survey { get; set; }
    }

}

0 个答案:

没有答案