2016-06-23 6 views
-1

フラグメントビューを使用してリストビューを設定しようとしています。 しかし、私はそれを実行するとアプリケーションがクラッシュします。もう一方のsend.IからJSONデータを取得しています。受信したデータをリストに追加してから更新するのはうんざりです。どんな助け?ここに私のコードです。ここでフラグメントを使用したListVewの作成

import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.SocketException; 
import java.util.ArrayList; 
import java.util.HashMap; 

public class OneFragment extends Fragment { 

    public OneFragment() { 
     // Required empty public constructor 
    } 

    private ProgressDialog pDialog; 

    // JSON Node names 
    private static final String TAG_CONTACTS = "us"; 
    private static final String TAG_NAME = "un"; 
    private static final String TAG_EMAIL = "ui"; 
    private static final String TAG = "JSON"; 

    int portnumber = 3245; 

    // contacts JSONArray 
    JSONArray contacts = null; 

    // Hashmap for ListView 
    ArrayList<HashMap<String, String>> contactList; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // setContentView(R.layout.fragment_one); 
    } 


    @Override 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootview = inflater.inflate(R.layout.fragment_one, container, false); 
     //View vew = inflater.inflate(R.layout.fragment_one, container, false); 
     new mparse().execute(); 
     return rootview; 
    } 

    /* @Override 
    public void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     new mparse().execute(); 
    } 
*/ 

    private class mparse extends AsyncTask<Void,Void,Void> { 

     private ProgressDialog pDialog = new ProgressDialog(getActivity()); 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 

      DatagramSocket socket = null; 
      DatagramPacket p; 
      byte message[] = new byte[1000]; 
      String jsonStr = ""; 
      p = new DatagramPacket(message, message.length); 
      JSONObject jsonObj = null; 
      try { 
       Log.d(TAG, "about to create the socket"); 
       socket = new DatagramSocket(portnumber); 
       if (socket == null) 
        Log.d("JSON", "Could not create socket"); 
       socket.receive(p); 
       jsonStr = new String(p.getData()); 
       Log.d(TAG, "the received string is" + jsonStr); 

       if (jsonStr != null) { 
        try { 
         jsonObj = new JSONObject(jsonStr); 
         Log.d(TAG, "inside the json"); 
         // Getting JSON Array node 
         contacts = jsonObj.getJSONArray(TAG_CONTACTS); 

         // looping through All Contacts 
         for (int i = 0; i < contacts.length(); i++) { 
          JSONObject c = contacts.getJSONObject(i); 
          // String id = c.getString(TAG_ID) 
          String name = c.getString(TAG_NAME); 
          Log.d(TAG, "value of user name is" + name); 
          String email = c.getString(TAG_EMAIL); 
          Log.d(TAG, "value of user id is " + email); 

          // tmp hashmap for single contact 
          HashMap<String, String> contact = new HashMap<String, String>(); 

          // adding each child node to HashMap key => value 

          contact.put(TAG_NAME, name); 
          contact.put(TAG_EMAIL, email); 

          //adding contact to contact list 
          Log.d(TAG, "the value of TAG_NAME " + TAG_NAME); 
          Log.d(TAG, "the value of TAG_EMAIL " + TAG_EMAIL); 
          contactList.add(contact); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
         Log.d(TAG, "inside JSON eception"); 
        } 
       } else { 
        Log.e("ServiceHandler", "Couldn't get any data from the url"); 
       } 
      } catch (SocketException e) { 
       e.printStackTrace(); 
       Log.d(TAG, "INside socketexception"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       Log.d(TAG, "Inside the IOException"); 
      } 

      return null; 

     } 

      @Override 
      protected void onPostExecute (Void arg) { 
       //super.onPostExecute(result); 
       // Dismiss the progress dialog 
       if (pDialog.isShowing()) 
        pDialog.dismiss(); 
       /** 
       * Updating parsed JSON data into ListView 
       * */ 
       ListView mylistView = (ListView) getView().findViewById(R.id.list); 
       ListAdapter adapter = new SimpleAdapter(
         getActivity(), contactList, 
         R.layout.list_xml, new String[]{TAG_NAME, TAG_EMAIL, 
       }, new int[]{R.id.name, 
         R.id.email, R.id.mobile}); 
       ; 
       mylistView.setAdapter(adapter); 
      } 

     } 
} 

はログです:

Process: com.example.kgj5kor.material, PID: 18196 
    java.lang.NullPointerException 
      at com.example.kgj5kor.material.OneFragment$mparse.onPostExecute(OneFragment.java:174) 
      at com.example.kgj5kor.material.OneFragment$mparse.onPostExecute(OneFragment.java:77) 
      at android.os.AsyncTask.finish(AsyncTask.java:632) 
      at android.os.AsyncTask.access$600(AsyncTask.java:177) 
      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:157) 
      at android.app.ActivityThread.main(ActivityThread.java:5335) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
      at dalvik.system.NativeStart.main(Native Method) 

答えて

0

代わりonPostExecute()ListViewオブジェクトを作成するので、onCreateView()でそれを作成し、それを公開します。 getView().findViewByIdrootview.findViewByIdに置き換えてください。

ListView mylistView; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootview = inflater.inflate(R.layout.fragment_one, container, false); 
    mylistView = (ListView) rootview.findViewById(R.id.list); 
    new mparse().execute(); 
    return rootview; 
} 

contactListが初期化されることはありませんルナンBandeira

+0

は私がやったウル返事をありがとう。 – Kirthan

+0

あなたのログレポートを掲示 –

+0

ええ、私はログレポート – Kirthan

0

により示唆されるようonCreateView()でもごcontactListを初期化します。 onCreateView()メソッドで初期化し、もう一度やり直してください。

編集

私は可能性の問題を回避するために、クラスを書き直しました。あなたは属性を初期化しておらず、pDialogは両方のクラスにあります。また、私は大会が言う権利としてそれを作るためにMparsemparseクラスの名前を変更:uはまだそれを実行することはできませんよ提案されているよう

import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.SocketException; 
import java.util.ArrayList; 
import java.util.HashMap; 

public class OneFragment extends Fragment { 

    public OneFragment() { 
     // Required empty public constructor 
    } 

    private ProgressDialog pDialog; 

    // JSON Node names 
    private static final String TAG_CONTACTS = "us"; 
    private static final String TAG_NAME = "un"; 
    private static final String TAG_EMAIL = "ui"; 
    private static final String TAG = "JSON"; 

    int portnumber = 3245; 

    // contacts JSONArray 
    JSONArray contacts = null; 

    // Hashmap for ListView 
    ArrayList<HashMap<String, String>> contactList; 


    @Override 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_one, container, false); 
    } 


    @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     contactList = new ArrayList<>(); 
     new Mparse().execute(); 
    } 

    private class Mparse extends AsyncTask<Void,Void,Void> { 

     public Mparse() { 
      pDialog = new ProgressDialog(getActivity()); 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 

      DatagramSocket socket = null; 
      DatagramPacket p; 
      byte message[] = new byte[1000]; 
      String jsonStr = ""; 
      p = new DatagramPacket(message, message.length); 
      JSONObject jsonObj = null; 
      try { 
       Log.d(TAG, "about to create the socket"); 
       socket = new DatagramSocket(portnumber); 
       if (socket == null) 
        Log.d("JSON", "Could not create socket"); 
       socket.receive(p); 
       jsonStr = new String(p.getData()); 
       Log.d(TAG, "the received string is" + jsonStr); 

       if (jsonStr != null) { 
        try { 
         jsonObj = new JSONObject(jsonStr); 
         Log.d(TAG, "inside the json"); 
         // Getting JSON Array node 
         contacts = jsonObj.getJSONArray(TAG_CONTACTS); 

         // looping through All Contacts 
         for (int i = 0; i < contacts.length(); i++) { 
          JSONObject c = contacts.getJSONObject(i); 
          // String id = c.getString(TAG_ID) 
          String name = c.getString(TAG_NAME); 
          Log.d(TAG, "value of user name is" + name); 
          String email = c.getString(TAG_EMAIL); 
          Log.d(TAG, "value of user id is " + email); 

          // tmp hashmap for single contact 
          HashMap<String, String> contact = new HashMap<String, String>(); 

          // adding each child node to HashMap key => value 

          contact.put(TAG_NAME, name); 
          contact.put(TAG_EMAIL, email); 

          //adding contact to contact list 
          Log.d(TAG, "the value of TAG_NAME " + TAG_NAME); 
          Log.d(TAG, "the value of TAG_EMAIL " + TAG_EMAIL); 
          contactList.add(contact); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
         Log.d(TAG, "inside JSON eception"); 
        } 
       } else { 
        Log.e("ServiceHandler", "Couldn't get any data from the url"); 
       } 
      } catch (SocketException e) { 
       e.printStackTrace(); 
       Log.d(TAG, "INside socketexception"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       Log.d(TAG, "Inside the IOException"); 
      } 

      return null; 

     } 

      @Override 
      protected void onPostExecute (Void arg) { 
       super.onPostExecute(result); 
       //In case the Fragment isn't shown anymore. 
       if (!isAdded() || getView() == null) { 
        return; 
       } 
       // Dismiss the progress dialog 
       if (pDialog != null && pDialog.isShowing()) 
        pDialog.dismiss(); 
       /** 
       * Updating parsed JSON data into ListView 
       * */ 
       ListView mylistView = (ListView) getView().findViewById(R.id.list); 
       ListAdapter adapter = new SimpleAdapter(
         getActivity(), contactList, 
         R.layout.list_xml, new String[]{TAG_NAME, TAG_EMAIL, 
       }, new int[]{R.id.name, 
         R.id.email, R.id.mobile}); 
       ; 
       mylistView.setAdapter(adapter); 
      } 

     } 
} 
+0

で私の投稿を編集しましたありがとう:)あなたの返信は、しかし、まだ解決策を見つけることができません。 – Kirthan

+0

'super.onPostExecute()'行のコメントを外します。 –

+0

これはうまくいきません:( – Kirthan

関連する問題