替换片段后,Android bundle为null

时间:2016-02-06 21:53:10

标签: android-fragments android-studio android-volley android-bundle

我有两个片段,分别是VolleyFragment和AnotherFragment,将VolleyFragment设置为初始片段。在此片段中,捆绑数据显示正常。但是,当我去AnotherFragment时,应用程序崩溃了。它说这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
                                                                                    at com.example.rendell.volleyfragment.AnotherFragment.onCreateView(AnotherFragment.java:28)

MainActivity

public class MainActivity extends AppCompatActivity {

public static final String JSON_URL = "http://192.168.0.102/musicmania/music/getMF";

TextView id,name,email;
String[] ids, names, emails;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    id = (TextView) findViewById(R.id.id);
    name = (TextView) findViewById(R.id.name);
    email = (TextView) findViewById(R.id.email);

    sendRequest();


}

private void sendRequest(){

    StringRequest stringRequest = new StringRequest(JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String json){
    ParseJSON pj = new ParseJSON(json);
    pj.parseJSON();

    id.setText(ParseJSON.ids[0]);
    name.setText(ParseJSON.titles[0]);
    email.setText(ParseJSON.artists[0]);

    FragmentManager fragmentTransaction = getFragmentManager();
    android.app.FragmentTransaction transaction = fragmentTransaction.beginTransaction();
    VolleyFragment squadFragment = new VolleyFragment();
    AnotherFragment anotherFragment = new AnotherFragment();
    Bundle bundle = new Bundle();
    bundle.putStringArray("id", ParseJSON.ids);
    bundle.putStringArray("name", ParseJSON.titles);
    bundle.putStringArray("email", ParseJSON.artists);
    squadFragment.setArguments(bundle);
    anotherFragment.setArguments(bundle);
    transaction.replace(R.id.containerView, squadFragment);
    transaction.commit();
}

public void changeFragment(View view) {
    FragmentManager fragmentTransaction = getFragmentManager();
    android.app.FragmentTransaction transaction = fragmentTransaction.beginTransaction();
    AnotherFragment anotherFragment = new AnotherFragment();
    transaction.replace(R.id.containerView, anotherFragment);
    transaction.commit();
    }
}

VolleyFragment

public class VolleyFragment extends Fragment {

TextView id,name,email;

String[] ids,names,emails;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.volley_fragment, container, false);
    id = (TextView) view.findViewById(R.id.id);
    name = (TextView) view.findViewById(R.id.name);
    email = (TextView) view.findViewById(R.id.email);

    ids = getArguments().getStringArray("id");
    names = getArguments().getStringArray("name");
    emails = getArguments().getStringArray("email");

    //jsonArray.setIds(id);
    id.setText(/*jsonArray.getIds()*/ids[0]);
    name.setText(names[0]);
    email.setText(emails[0]);
    return view;
    }

}

AnotherFragment

public class AnotherFragment extends Fragment {

TextView id,name,email;

String[] ids,names,emails;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.another_fragment, container, false);
    id = (TextView) view.findViewById(R.id.id);
    name = (TextView) view.findViewById(R.id.name);
    email = (TextView) view.findViewById(R.id.email);

    ids = getArguments().getStringArray("id");
    names = getArguments().getStringArray("name");
    emails = getArguments().getStringArray("email");

    //jsonArray.setIds(id);
    id.setText(/*jsonArray.getIds()*/ids[0]);
    name.setText(names[0]);
    email.setText(emails[0]);
    return view;
    }
}

1 个答案:

答案 0 :(得分:0)

我无法完全按照您在此处所做的事情,但我发现了许多问题:

  • 在showJSON中,您新建了一个ParseJSON对象并将其分配给pj,但从不使用pj。相反,您会静态到达ParseJSON以获取填充Bundle的值。这看起来很奇怪。我希望pj实例包含数据。
  • 通常一个人没有&#34;新&#34;一个片段。通常的模式是在片段本身的类中使用静态构建器。
  • 您将同一个Bundle传递给两个不同的片段。除非你知道一个事实是好的,否则请尝试为每一个使用不同的Bundle。
  • showJSON中的
  • ,你的AnotherFragment实例似乎并没有被使用
  • 我没有看到任何名为changeFragment的地方
  • 您在Bundle中传递字符串数组,但您只使用片段中数组中的第一个元素。考虑只传递一个字符串?