Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将JSON数据从活动传递到片段_Android_Android Fragments_Bundle - Fatal编程技术网

Android 如何将JSON数据从活动传递到片段

Android 如何将JSON数据从活动传递到片段,android,android-fragments,bundle,Android,Android Fragments,Bundle,当我将JSON数据从活动传递到片段时,它不起作用。 我想获得两个数据(名称和CarRemaining)并显示在RecyclerView中。 如何改进代码?谢谢 public class Search extends MainActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC

当我将JSON数据从活动传递到片段时,它不起作用。 我想获得两个数据(名称和CarRemaining)并显示在RecyclerView中。 如何改进代码?谢谢

public class Search extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_search);
        ...
        String url = "API...";
        new ParkTask().execute(url);
    }

    class ParkTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        StringBuilder json = new StringBuilder();

        try {
            URL url = new URL(params[0]);
            Log.d(TAG, "AsyncTask doInBackground: " + params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream is = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = br.readLine();
            while (line != null) {
                json.append(line);
                line = br.readLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return json.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        JSONObject jonObject = null;
        List<Park> parks = new ArrayList<>();
        Bundle bundle = new Bundle();

        try {
            jonObject = new JSONObject(result);
            JSONArray recordArray = jonObject.getJSONArray("ParkingInfoList");

            for (int i = 0; i < recordArray.length(); i++) {
                JSONObject object = recordArray.getJSONObject(i);
                String name = object.getString("Name");
                String tel=object.getString("Tel");
                String address = object.getString("Address");
                String carTotal = object.getString("CarTotal");
                String carRemaining = object.getString("CarRemaining");
                String serviceTime = object.getString("ServiceTime");
                String chargeInfo = object.getString("ChargeInfo");
                String parkingInfo = object.getString("ParkingInfo");
                String lat = object.getString("Lat");
                String lng = object.getString("Lng");
                parks.add(new Park(name, address, tel, carTotal, carRemaining,
                        serviceTime, chargeInfo, parkingInfo, lat, lng));
                Log.d(TAG, "AsyncTask onPostExecute: " +
                        name + tel + address + carTotal +
                        carRemaining + serviceTime + chargeInfo +
                        parkingInfo + lat + lng );
                bundle.putString("key",object.toString());
            }
            bundle.putString("key",jonObject.toString());
            DesignatedFragment designatedFragment = new DesignatedFragment();
            designatedFragment.setArguments(bundle);
            getSupportFragmentManager().beginTransaction().replace(R.id.recycler,designatedFragment).commit();


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' 关于空对象引用

这是我的片段

public class DesignatedFragment extends Fragment{
    List<Park> parks = new ArrayList<>();
.
.
.
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle bundle=getArguments();
        String jsonString=bundle.getString("key");
        JSONObject object;
        try {
            object=new JSONObject(jsonString);
            recycler.setAdapter(new ParkAdapter(parks));
        }catch (JSONException e){
            e.printStackTrace();
        }

        return inflater.inflate(R.layout.fragment_designated, container, false);
    }
}
公共类指定片段扩展片段{
List parks=new ArrayList();
.
.
.
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
Bundle=getArguments();
String jsonString=bundle.getString(“key”);
JSONObject对象;
试一试{
对象=新的JSONObject(jsonString);
回收商。setAdapter(新ParkAdapter(parks));
}捕获(JSONException e){
e、 printStackTrace();
}
返回充气机。充气(R.layout.fragment_指定,容器,假);
}
}
我使用RecyclerView显示数据

fragment_designed.xml

<androidx.constraintlayout.widget.ConstraintLayout 
...
    tools:context=".DesignatedFragment">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        tools:ignore="MissingConstraints" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="49dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <TextView
        android:id="@+id/parkSpace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="17dp"
        android:textColor="#464646"
        android:textSize="24sp"
        android:textStyle="normal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        tools:text="Car Remaining" />

    <TextView
        android:id="@+id/parkName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="17dp"
        android:textColor="#464646"
        android:textSize="18sp"
        android:textStyle="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints"
        tools:text="ParkName" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:layout_marginEnd="11dp"
        android:layout_marginRight="11dp"
        android:layout_marginBottom="44dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/kit_right"
        tools:ignore="ContentDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>

另一个row.xml

<androidx.constraintlayout.widget.ConstraintLayout 
...
    tools:context=".DesignatedFragment">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        tools:ignore="MissingConstraints" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="49dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <TextView
        android:id="@+id/parkSpace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="17dp"
        android:textColor="#464646"
        android:textSize="24sp"
        android:textStyle="normal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        tools:text="Car Remaining" />

    <TextView
        android:id="@+id/parkName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="17dp"
        android:textColor="#464646"
        android:textSize="18sp"
        android:textStyle="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints"
        tools:text="ParkName" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:layout_marginEnd="11dp"
        android:layout_marginRight="11dp"
        android:layout_marginBottom="44dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/kit_right"
        tools:ignore="ContentDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>

将类Park扩展为可序列化或可打包,然后可以将捆绑包中的对象传递给片段

public class DesignatedFragment extends Fragment{
    List<Park> parks = new ArrayList<>();
.
.
.
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle bundle=getArguments();
        String jsonString=bundle.getString("key");
        JSONObject object;
        try {
            object=new JSONObject(jsonString);
            recycler.setAdapter(new ParkAdapter(parks));
        }catch (JSONException e){
            e.printStackTrace();
        }

        return inflater.inflate(R.layout.fragment_designated, container, false);
    }
}

另一个解决方案是将对象类转换为字符串,然后恢复为对象类