をアクティビティを変更した後にNullPointerExceptionエラーが発生します。これは、その下の断片を使用して、別のフラグメントDisplayinListViewにつながるですjson_string = getActivity().getIntent().getExtras().getString("json_data");
は私がフラグメントするには、以下の
09-10 21:56:07.507 22129-22129/com.example.aids.a09application E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aids.a09application, PID: 22129
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.example.aids.a09application.DisplayListView.onCreateView(DisplayListView.java:31)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
、私は取得していますエラーのためLogcatですこのコードの最後に見つかったフラグメントトランザクション。もともと、これは、DisplayinListViewがアクティビティであったのと同じ意図でしたが、ナビゲーション・ドロワのために、これをフラグメントに変更しました。
public class StandingsList extends Fragment implements View.OnClickListener {
//make member variable is Views
Button mButton;
Button mButton1;
TextView mResult;
String JSON_RESPONSE;
String json_string;
FragmentTransaction fragmentTransaction;
ProgressDialog mProgressDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_standings, container, false);
//get reference of the views
mButton = (Button) view.findViewById(R.id.button);
mButton1 = (Button) view.findViewById(R.id.buttontwo);
mResult = (TextView) view.findViewById(R.id.result);
mButton1.setOnClickListener(this);
//when button is clicked
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//call the getJsonResponse method and fetch the response from the server
new getJsonResponse().execute();
}
});
return view;
}
public class getJsonResponse extends AsyncTask<Void, Void, String> {
String serverUrl;
public getJsonResponse() {
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Please Wait");
mProgressDialog.setTitle("Processing");
mProgressDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
//set the url from we have to fetch the json response
// DO NOT ADD REAL IP.
serverUrl = "http://xx.xx.xx.xx/get_info.php";
mProgressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL(serverUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while ((JSON_RESPONSE = bufferedReader.readLine()) != null) {
stringBuilder.append(JSON_RESPONSE + "\n");
}
inputStream.close();
bufferedReader.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e); //print exception message to log
} catch (IOException e) {
Log.e(TAG, "IOException: " + e); //print exception message to log
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
//set the result which is returned by doInBackground() method to result textView
mResult.setText(result);
mProgressDialog.dismiss();
json_string = result;
}
}
public void onClick(View view){
switch (view.getId()) {
case R.id.buttontwo:
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(main_container, new DisplayListView());
fragmentTransaction.commit();
break;
}
}
}
これは、エラーは、ライン31上にあるクラスである - 私は活動からのフラグメントに、このクラスを変換するのでjson_string = getActivity().getIntent().getExtras().getString("json_data");
このエラーのみ作成されています。
public class DisplayListView extends Fragment {
String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
StandingsAdapter standingsAdapter;
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.display_list_view_layout, container, false);
listView= (ListView) view.findViewById(R.id.ListViewParse);
standingsAdapter = new StandingsAdapter(getActivity(),R.layout.row_layout);
listView.setAdapter(standingsAdapter);
json_string = getActivity().getIntent().getExtras().getString("json_data");
try {
jsonArray = new JSONObject(json_string).getJSONArray("server_response");
int count = 0;
int driver_id, team_id, position, points;
String firstName, lastName;
while(count<jsonArray.length())
{
JSONObject JO = jsonArray.getJSONObject(count);
driver_id = JO.getInt("Driver_id");
team_id =JO.getInt("Team_id");
firstName = JO.getString("First_name");
lastName= JO.getString("Last_name");
position= JO.getInt("Position");
points = JO.getInt("Points");
Standings standings = new Standings(driver_id, team_id, firstName, lastName, position, points);
standingsAdapter.add(standings);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
return view;
}
}
おかげで以下のようにフラグメントののonCreateメソッド内の引数から値を取得することができます。これで、JSONArray = new JSONObject(json_string).getJSONArray( "server_response");でエラーが発生しました。 – user8534232
別の質問を投稿する私の解決策があなたの答えを選択してくれた場合 – phpdroid