以下是我收到的错误的Logcat 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)
这是下面的片段,它将导致另一个Fragment DisplayinListView,使用在此代码底部找到的片段事务。最初这是一个意图,因为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;
}
}
答案 0 :(得分:1)
替换
BIN2
通过
json_string = getActivity().getIntent().getExtras().getString( "json_data" );
答案 1 :(得分:1)
现在,您已用新Fragment替换了您的活动。所以您必须将数据作为参数传递。您可以按照我在下面的代码中提到的那样做。
Bundle bundle = new Bundle();
bundle.putString(Constants.STRING_JSON, json_string);
replaceFragment(DisplayListView.newInstance(bundle));
现在,您必须在下面的实际fragnent广告中获取此json_string。
public static DisplayListView newInstance(Bundle bungle) {
DisplayListView fragment = new DisplayListView();
fragment.setArguments(bungle);
return fragment;}
您可以从片段的Oncreate方法中的参数中检索值,如下所示
String json_string = getArguments().getString(Constants.STRING_JSON);