2017-06-29 19 views
0

は、私は数時間のために解決することができなかった問題に遭遇しているとコミュニティが助けることができる期待しています。アンドロイド、JSON解析エラー

、このアプリケーションのポイントは、JSONデータを取得し、唯一のサムネイルURLとタイトルが表示されますカスタムリストビューに表示されます。

JSONの構文解析エラーが何らかの原因で停止しているように見えることから、カスタムリストビューを呼び出して設定しようとすると、空の文字列配列が渡されていますJSONの解析。

問題のエラーは、次のようになります。

E/Zygote: v2 
I/libpersona: KNOX_SDCARD checking this for 10010 
I/libpersona: KNOX_SDCARD not a persona 
E/Zygote: accessInfo : 0 
W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2], Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0005, [-1 -1 -1 -1 0 1] 
I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.example.user.remotemachineuilayout 
I/art: Late-enabling -Xcheck:jni 
D/TimaKeyStoreProvider: TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service 
06-29 09:22:16.042 31340-31350/? I/art: Debugger is no longer active 
06-29 09:22:16.042 31340-31350/? I/art: Starting a blocking GC Instrumentation 
06-29 09:22:16.166 31340-31340/? W/System: ClassLoader referenced unknown path: /data/app/com.example.user.remotemachineuilayout-1/lib/arm64 
06-29 09:22:16.185 31340-31340/? I/InstantRun: starting instant run server: is main process 
06-29 09:22:16.247 31340-31340/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
06-29 09:22:16.316 31340-31340/? D/AbsListView: Get MotionRecognitionManager 
06-29 09:22:16.317 31340-31340/? D/MotionRecognitionManager: mSContextService = com[email protected]ed99b7a 
06-29 09:22:16.318 31340-31340/? D/MotionRecognitionManager: motionService = c[email protected]a628f2b 
06-29 09:22:16.318 31340-31340/? D/MotionRecognitionManager: motionService = c[email protected]a628f2b 
06-29 09:22:16.323 31340-31340/? D/AndroidRuntime: Shutting down VM 
06-29 09:22:16.323 31340-31340/? E/AndroidRuntime: FATAL EXCEPTION: main 
                Process: com.example.user.remotemachineuilayout, PID: 31340 
                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.remotemachineuilayout/com.example.user.remotemachineuilayout.MainActivity}: java.lang.NullPointerException 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
                 at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                 at android.os.Looper.loop(Looper.java:154) 
                 at android.app.ActivityThread.main(ActivityThread.java:6682) 
                 at java.lang.reflect.Method.invoke(Native Method) 
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
                Caused by: java.lang.NullPointerException 
                 at java.util.Objects.requireNonNull(Objects.java:203) 
                 at java.util.Arrays$ArrayList.<init>(Arrays.java:3826) 
                 at java.util.Arrays.asList(Arrays.java:3813) 
                 at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:139) 
                 at com.example.user.remotemachineuilayout.CustomAdapter.<init>(CustomAdapter.java:0) 
                 at com.example.user.remotemachineuilayout.MainActivity.onCreate(MainActivity.java:49) 
                 at android.app.Activity.performCreate(Activity.java:6942) 
                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)  
                 at android.app.ActivityThread.-wrap14(ActivityThread.java)  
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)  
                 at android.os.Handler.dispatchMessage(Handler.java:102)  
                 at android.os.Looper.loop(Looper.java:154)  
                 at android.app.ActivityThread.main(ActivityThread.java:6682)  
                 at java.lang.reflect.Method.invoke(Native Method)  
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)  
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)  
06-29 09:22:16.325 31340-31391/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default 
06-29 09:22:16.327 31340-31391/? I/System.out: (HTTPLog)-Static: isSBSettingEnabled false 
06-29 09:22:16.327 31340-31391/? I/System.out: (HTTPLog)-Static: isSBSettingEnabled false 
06-29 09:22:16.512 31340-31391/? E/MainActivity: Response from url: [ 
                { 
                "albumId": 1, 
                "id": 1, 
                "title": "accusamus beatae ad facilis cum similique qui sunt", 
                "url": "http://placehold.it/600/92c952", 
                "thumbnailUrl": "http://placehold.it/150/92c952" 
                }, 
                { 
                "albumId": 1, 
                "id": 2, 
                "title": "reprehenderit est deserunt velit ipsam", 
                "url": "http://placehold.it/600/771796", 
                "thumbnailUrl": "http://placehold.it/150/771796" 
                }, 
                { 
                "albumId": 1, 
                "id": 3, 
                "title": "officia porro iure quia iusto qui ipsa ut modi", 
                "url": "http://placehold.it/600/24f355", 
                "thumbnailUrl": "http://placehold.it/150/24f355" 
                }, 
                { 
                "albumId": 1, 
                "id": 4, 
                "title": "culpa odio esse rerum omnis laboriosam voluptate repudiandae", 
                "url": "http://placehold.it/600/d32776", 
                "thumbnailUrl": "http://placehold.it/150/d32776" 
                }, 
                { 
                "albumId": 1, 
                "id": 5, 
                "title": "natus nisi omnis corporis facere molestiae rerum in", 
                "url": "http://placehold.it/600/f66b97", 
                "thumbnailUrl": "http://placehold.it/150/f66b97" 
                }, 
                { 
                "albumId": 1, 
                "id": 6, 
                "title": "accusamus ea aliquid et amet sequi nemo", 
                "url": "http://placehold.it/600/56a8c2", 
                "thumbnailUrl": "http://placehold.it/150/56a8c2" 
                }, 
                { 
                "albumId": 1, 
                "id": 7, 
                "title": "officia delectus consequatur vero aut veniam explicabo molestias", 
                "url": "http://placehold.it/600/b0f7cc", 
                "thumbnailUrl": "http://placehold.it/150/b0f7cc" 
                }, 
                { 
                "albumId": 1, 
                "id": 8, 
                "title": "aut porro officiis laborum odit ea laudantium corporis", 
                "url": "http://placehold.it/600/54176f", 
                "thumbnailUrl": "http://placehold.it/150/54176f" 
                }, 
                { 
                "albumId": 1, 
                "id": 9, 
                "title": "qui eius qui autem sed", 
                "url": "http://placehold.it/600/51aa97", 
                "thumbnailUrl": "http://placehold.it/150/51aa97" 
                }, 
                { 
                "albumId": 1, 
                "id": 10, 
                "title": "beatae et provident et ut vel", 
                "url": "http://placehold.it/600/810b14", 
                "thumbnailUrl": "http://placehold.it/150/810b14" 
                }, 
                { 
                "albumId": 1, 
                "id": 11, 
                "title": "nihil at amet non hic quia qui", 
                "url": "http://placehold.it/600/1ee8a4", 
                "thumbnailUrl": "http://placehold.it/150/1ee8a4" 
                }, 
                { 
                "albumId": 1, 
                "id": 12, 
                "title": "mollitia soluta ut rerum eos aliquam consequatur perspiciatis maiores", 
                "url": "http://placehold.it/600/66b7d2", 
                "thumbnailUrl": "http://placehold.it/150/66b7d2" 
                }, 
                { 
                "albumId": 1, 
                "id": 13, 
                "title": "repudiandae iusto deleniti rerum", 
                "url": "http://placehold.it/600/197d29", 
                "thumbnailUrl": "http://placehold.it/150/197d29" 
                }, 
                { 
                "albumId": 1, 
                "id": 14, 
                "title": "est necessitatibus architecto ut laborum", 
                "url": "http://placehold.it/600/61a65", 
                "thumbnailUrl": "http://placehold.it/150/61a65" 
                }, 
                { 
                "albumId": 1, 
                "id": 15, 
                "title": "harum dicta similique quis dolore earum ex qui", 
                "url": "http://placehold.it/600/f9cee5", 
                "thumbnailUrl": "http://placehold.it/150/f9cee5" 
                }, 
                { 
                "albumId": 1, 
                "id": 16, 
                "title": "iusto sunt nobis quasi veritatis quas expedita voluptatum deserunt", 
                "url": "http://placehold.it/600/fdf73e", 
                "thumbnailUrl": "http://placehold.it/150/fdf73e" 
                }, 
                { 
                "albumId": 1, 
                "id": 17, 
                "title": "natus doloribus necessitatibus ipsa", 
                "url": "http://placehold.it/600/9c184f", 
                "thumbnailUrl": "http://placehold.it/150/9c184f" 
                }, 
                { 
                "albumId": 1, 
                "id": 18, 
                "title": "laboriosam odit nam necessitatibus et illum dolores reiciendis", 
                "url": "http://placehold.it/600/1fe46f", 
                "thumbnailUrl": "http://placehold.it/150/1fe46f" 
                }, 
                { 
                "albumId": 1, 
                "id": 19, 
                "title": "perferendis nesciunt eveniet et optio a", 
                "url": "http://placehold.it/600/56acb2", 
                "thumbnailUrl": "http://placehold.it/150/56acb2" 
                }, 
                { 
                "albumId": 1, 
                "id": 20, 
                "title": "assumenda voluptatem laboriosam enim consequatur veniam placeat reiciendis error", 
                "url": "http://placehold.it/600/8985dc", 
                "thumbnailUrl": "http://p 
06-29 09:22:16.775 31340-31391/? E/MainActivity: Json parsing error: Value [{"albumId":1,"id":1,"title":"accusamus beatae ad facilis cum similique qui sunt","url":"http:\/\/placehold.it\/600\/92c952","thumbnailUrl":"http:\/\/placehold.it\/150\/92c952"},{"albumId":1,"id":2,"title":"reprehenderit est deserunt velit ipsam","url":"http:\/\/placehold.it\/600\/771796","thumbnailUrl":"http:\/\/placehold.it\/150\/771796"},{"albumId":1,"id":3,"title":"officia porro iure quia iusto qui ipsa ut modi","url":"http:\/\/placehold.it\/600\/24f355","thumbnailUrl":"http:\/\/placehold.it\/150\/24f355"},{"albumId":1,"id":4,"title":"culpa odio esse rerum omnis laboriosam voluptate repudiandae","url":"http:\/\/placehold.it\/600\/d32776","thumbnailUrl":"http:\/\/placehold.it\/150\/d32776"},{"albumId":1,"id":5,"title":"natus nisi omnis corporis facere molestiae rerum in","url":"http:\/\/placehold.it\/600\/f66b97","thumbnailUrl":"http:\/\/placehold.it\/150\/f66b97"},{"albumId":1,"id":6,"title":"accusamus ea aliquid et amet sequi nemo","url":"http:\/\/placehold.it\/600\/56a8c2","thumbnailUrl":"http:\/\/placehold.it\/150\/56a8c2"},{"albumId":1,"id":7,"title":"officia delectus consequatur vero aut veniam explicabo molestias","url":"http:\/\/placehold.it\/600\/b0f7cc","thumbnailUrl":"http:\/\/placehold.it\/150\/b0f7cc"},{"albumId":1,"id":8,"title":"aut porro officiis laborum odit ea laudantium corporis","url":"http:\/\/placehold.it\/600\/54176f","thumbnailUrl":"http:\/\/placehold.it\/150\/54176f"},{"albumId":1,"id":9,"title":"qui eius qui autem sed","url":"http:\/\/placehold.it\/600\/51aa97","thumbnailUrl":"http:\/\/placehold.it\/150\/51aa97"},{"albumId":1,"id":10,"title":"beatae et provident et ut vel","url":"http:\/\/placehold.it\/600\/810b14","thumbnailUrl":"http:\/\/placehold.it\/150\/810b14"},{"albumId":1,"id":11,"title":"nihil at amet non hic quia qui","url":"http:\/\/placehold.it\/600\/1ee8a4","thumbnailUrl":"http:\/\/placehold.it\/150\/1ee8a4"},{"albumId":1,"id":12,"title":"mollitia soluta ut rerum eos aliquam consequatur perspiciatis maiores","url":"http:\/\/placehold.it\/600\/66b7d2","thumbnailUrl":"http:\/\/placehold.it\/150\/66b7d2"},{"albumId":1,"id":13,"title":"repudiandae iusto deleniti rerum","url":"http:\/\/placehold.it\/600\/197d29","thumbnailUrl":"http:\/\/placehold.it\/150\/197d29"},{"albumId":1,"id":14,"title":"est necessitatibus architecto ut laborum","url":"http:\/\/placehold.it\/600\/61a65","thumbnailUrl":"http:\/\/placehold.it\/150\/61a65"},{"albumId":1,"id":15,"title":"harum dicta similique quis dolore earum ex qui","url":"http:\/\/placehold.it\/600\/f9cee5","thumbnailUrl":"http:\/\/placehold.it\/150\/f9cee5"},{"albumId":1,"id":16,"title":"iusto sunt nobis quasi veritatis quas expedita voluptatum deserunt","url":"http:\/\/placehold.it\/600\/fdf73e","thumbnailUrl":"http:\/\/placehold.it\/150\/fdf73e"},{"albumId":1,"id":17,"title":"natus doloribus necessitatibus ipsa","url":"http:\/\/placehold.it\/600\/9c184f","thumbnailUrl":"http:\/\/placehold.it\/150\/9c184f"},{"albumId":1,"id":18,"title":"laboriosam odit nam necessitatibus et illum dolores reiciendis","url":"http:\/\/placehold.it\/600\/1fe46f","thumbnailUrl":"http:\/\/placehold.it\/150\/1fe46f"},{"albumId":1,"id":19,"title":"perferendis nesciunt eveniet et optio a","url":"http:\/\/placehold.it\/600\/56acb2","thumbnailUrl":"http:\/\/placehold.it\/150\/56acb2"},{"albumId":1,"id":20,"title":"assumenda voluptatem laboriosam enim consequatur veniam placeat reiciendis error","url":"http:\/\/placehold.it\/600\/8985dc","thumbnailUrl":"http:\/\/placehold.it\/150\/8985dc"},{"albumId":1,"id":21,"title":"ad et natus qui","url":"http:\/\/placehold.it\/600\/5e12c6","thumbnailUrl":"http:\/\/placehold.it\/150\/5e12c6"},{"albumId":1,"id":22,"title":"et ea illo et sit voluptas animi blanditiis porro","url":"http:\/\/placehold.it\/600\/45601a","thumbnailUrl":"http:\/\/placehold.it\/150\/45601a"},{"albumId":1,"id":23,"title":"harum velit vero totam","url":"http:\/\/placehold.it\/600\/e924e6","thumbnailUrl":"http:\/\/placehold.it\/150\/e924e6"},{"albumId":1,"id":24,"title":"beatae officiis ut aut"," 

私のコードは以下の通りです。

MainActivity

package com.example.user.remotemachineuilayout; 

import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.webkit.HttpAuthHandler; 
import android.widget.ListView; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.Toast; 

import android.util.Log; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.util.HashMap; 
import java.io.IOException; 
import java.util.ArrayList; 
import org.apache.http.client.HttpClient; 

public class MainActivity extends AppCompatActivity { 


    String[] photoLast; 
    String [] titleLast; 
    ArrayList<String> urlPhoto = new ArrayList<String>(); 
    ArrayList<String> titleName = new ArrayList<String>(); 
    ListView jsonDisplay; 
    CustomAdapter displayAdapter; 

    final static String URL = "http://jsonplaceholder.typicode.com/photos"; 
    private String TAG = MainActivity.class.getSimpleName(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     new Read().execute(); 
     displayAdapter = new CustomAdapter(MainActivity.this,titleLast, photoLast); 
     jsonDisplay = (ListView) findViewById(R.id.jsonDisplay); 
     jsonDisplay.setAdapter(displayAdapter); 

//  jsonDisplay = (ListView) findViewById(R.id.jsonDisplay); 
//  displayAdapter = new CustomAdapter(this, title); 
//  jsonDisplay.setAdapter(displayAdapter); 
    } 



    public class Read extends AsyncTask <Void, Void, Void> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      //TODO Auto-generated method stub 
      HttpHandler sh = new HttpHandler(); 
      String url = URL; 
      String jsonStr = sh.makeServiceCall(url); 



      Log.e(TAG, "Response from url: " + jsonStr); 
      if(jsonStr != null) { 
       try { 
        JSONObject json = new JSONObject(jsonStr); 

        JSONArray photos = json.getJSONArray("photos"); 
        photoLast = new String[photos.length()]; 
        titleLast = new String[photos.length()]; 
        for (int i = 0; i < photos.length(); i++) { 
         JSONObject temp = photos.getJSONObject(i); 
         String title = temp.getString("title"); 
         String thumbNailURL = temp.getString("thumbnailUrl"); 

         photoLast[i]=thumbNailURL; 
         titleLast[i]=title; 
//      urlPhoto.add(thumbNailURL); 
//      titleName.add(title); 

        } 
       } catch (final JSONException e) { 
        Log.e(TAG, "Json parsing error: " + e.getMessage()); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), 
            "Json parsing error2: " + e.getMessage(), 
            Toast.LENGTH_LONG).show(); 
         } 
        }); 
       } 
       } else{ 
        Log.e(TAG, "Couldn't get json from server."); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), 
            "Couldn't get json from server. Check LogCat for possible errors!", 
            Toast.LENGTH_LONG).show(); 
         } 
        }); 
       } 

      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result){ 
      //TODO Auto-Generated method stub 
      //photoLast = urlPhoto.toArray(photoLast); 
      //titleLast = titleName.toArray(titleLast); 
      super.onPostExecute(result); 


     } 



    } 
} 

のHttpHandler(過去のテキストの制限私は、コードを持ってどこに入れて、削除する必要があります)

https://www.tutorialspoint.com/android/android_json_parser.htm 

CustomAdapter

package com.example.user.remotemachineuilayout; 

/** 
* Created by User on 6/29/2017. 
*/ 

import android.content.Context; 
import android.support.annotation.LayoutRes; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.view.View; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.ArrayAdapter; 
import android.app.Activity; 

import com.squareup.picasso.Picasso; 
import java.util.ArrayList; 
import static com.example.user.remotemachineuilayout.R.id.url; 


public class CustomAdapter extends ArrayAdapter<String> { 

    //private final Activity context; 
    private final String[] titleList; 
    private final String[] urlList; 



    public CustomAdapter(Context context1, String[] title, String[] url) { 
     super(context1, R.layout.custom_row, title); 
     //this.context = context1; 
     this.titleList = title; 
     this.urlList = url; 
    } 

    @NonNull 
    @Override 
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     View rowView = inflater.inflate(R.layout.custom_row,parent,false); 
     TextView txtTitle = (TextView) rowView.findViewById(R.id.title); 
     ImageView imageView = (ImageView) rowView.findViewById(url); 

     txtTitle.setText(titleList[position]); 
     Picasso.with(this.getContext()).load(urlList[position]).into(imageView); 


     return rowView; 
    } 
} 
+0

最初の 'new Read()。execute();'は非同期で、2番目にあなたの応答に '写真'のjsonarrayを見ることができません。 –

+0

あなたの権利と別のユーザもそれを指摘しました。頭がおかげでありがとう、私はなぜ私も写真jsonarrayを入れてもわからない – user3051442

+0

明らかにnullポインタの例外。間違った名前から値を取得しているか、jsonに抽出したいものが含まれていません。 –

答えて

1
if(jsonStr != null) { 
     try { 
      JSONArray photos = new JSONArray(jsonStr); 
      photoLast = new String[photos.length()]; 
      titleLast = new String[photos.length()]; 
      for (int i = 0; i < photos.length(); i++) { 
       JSONObject temp = photos.getJSONObject(i); 
       String title = temp.getString("title"); 
       String thumbNailURL = temp.getString("thumbnailUrl"); 

       photoLast[i]=thumbNailURL; 
       titleLast[i]=title; 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

このコードを試してみてください。 "写真"と呼ばれるjson配列が表示されません

+0

ああ、私はそれらの写真をどこから入手したのか知りませんか?完璧に今あなたに感謝します。 – user3051442

0

オフのNullPointerExectionが原因です。 AsyncTaskは、あなたがそのように使用するときにそう

new Read().execute(); 
     displayAdapter = new CustomAdapter(MainActivity.this,titleLast, photoLast); 
     jsonDisplay = (ListView) findViewById(R.id.jsonDisplay); 
     jsonDisplay.setAdapter(displayAdapter); 

、titleLastとphotoLastはnullになります別のスレッドで実行されます。最後の3行をReadのonPostExecute関数に移動するとこの問題が解決されます。

+0

実際に私はそれらを以前にも持っていましたが、それでも解析エラーが発生しました。同じ時点で切断されました – user3051442