Java 当我尝试将JSON解析为列表视图时,我收到一个ResourceNotFound异常?
尝试将我的Java 当我尝试将JSON解析为列表视图时,我收到一个ResourceNotFound异常?,java,android,json,Java,Android,Json,尝试将我的JSON信息解析为ListView时,我一直收到的错误 09-10 19:31:02.560 11200-11200/com.example.aids.a09application E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aids.a09application, PID: 11200 android.content.res.Resources$NotFoundException: String re
JSON
信息解析为ListView
时,我一直收到的错误
09-10 19:31:02.560 11200-11200/com.example.aids.a09application E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aids.a09application, PID: 11200
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:1178)
at android.widget.TextView.setText(TextView.java:5157)
at com.example.aids.a09application.StandingsAdapter.getView(StandingsAdapter.java:65)
at android.widget.AbsListView.obtainView(AbsListView.java:3229)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1396)
at android.widget.ListView.onMeasure(ListView.java:1303)
at android.view.View.measure(View.java:21046)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:21046)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:21046)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:21046)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21046)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:21046)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:785)
at android.view.View.measure(View.java:21046)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2562)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1629)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1878)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1509)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7051)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:702)
at android.view.Choreographer.doFrame(Choreographer.java:638)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
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
说明错误是我的StandingsAdapter
类的第65行,如下所示。第66行是standingsHolder.txt_id.setText(standings.getTeam_id())代码>
我将发布android应用程序中涉及此错误的其他类。下面是我的排名课程
public class Standings {
private int driver_id;
private int team_id;
private String firstName;
private String lastName;
private int position;
private int points;
public Standings(int driver_id, int team_id, String firstName, String lastName, int position, int points) {
this.setDriver_id( driver_id );
this.setTeam_id( team_id );
this.setFirstName( firstName );
this.setLastName( lastName );
this.setPosition( position );
this.setPoints( points );
}
public int getDriver_id() {
return driver_id;
}
public void setDriver_id(int driver_id) {
this.driver_id = driver_id;
}
public int getTeam_id() {
return team_id;
}
public void setTeam_id(int team_id) {
this.team_id = team_id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
}
下面是我的StandingList片段
,这是主片段
,在这里可以找到下载JSON
和解析JSON
的按钮
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;
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
serverUrl = "http://163.172.142.145/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:
Intent intent = new Intent(getActivity(), DisplayListView.class);
intent.putExtra( "json_data",json_string );
startActivity( intent );
break;
}
}
}
验证您是否正确提取了姓名
例如:
firstName = JO.getString( "First_name" );
逐步检查或检查JSON是否正确提取和存储:
jsonArray = new JSONObject(json_string).getJSONArray("server_response");
标记是否正确?您需要使用Integer.toString(Intargument)将int值转换为String
检查我今天早些时候的回答,注意伊万,先生。谢谢你帮我解决问题
firstName = JO.getString( "First_name" );
jsonArray = new JSONObject(json_string).getJSONArray("server_response");
Standings standings = (Standings)this.getItem( position );
standingsHolder.tx_id.setText( Integer.toString(standings.getDriver_id()) );
standingsHolder.txt_id.setText(Integer.toString(standings.getTeam_id()));
standingsHolder.tx_fn.setText( standings.getFirstName() );
standingsHolder.tx_ln.setText( standings.getLastName() );
standingsHolder.tx_pos.setText(Integer.toString(standings.getPosition()));
standingsHolder.tx_po.setText(Integer.toString(standings.getPoints()));