2017-05-08 11 views
0

このページは単なるリストビューで、この場合のアイテムをこのリストのlistviewに表示します。メッセージの受信トレイに同じコードを使用しました。メッセージのリストを返す以外は同じことを行いました。およびAPIを使用してリストビューをリストビューに表示しようとしています

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

これは次のとおりですが、この1つは動作していないよう、私はページをロードすると、私はこのエラーを取得する知識が限られているため、エラーを理解していないか、どこでも

を見てページのコード:

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

'List survey 'に値が含まれているかどうかチェックしていますか? –

+0

アイテムがAPI呼び出しからのアンケートに追加されます。ループを参照してください –

答えて

1

あなたのArrayListの項目の1つがnullだと思います。そのため、その項目までスクロールするときにNullPointerExceptionが発生します。 ListViewに項目を追加する前に、項目がnullであるかどうかを確認する方が良いでしょう。

for (int c = 0; c < survey.Count; c++) 
{ 
if(survey[c].survey != null)   
surveys.Add(survey[c].survey); 
} 
関連する問題