gpt4 book ai didi

java - 当我尝试将我的 JSON 解析为 ListView 时,我收到“未找到资源”异常?

转载 作者:太空狗 更新时间:2023-10-29 13:51:41 25 4
gpt4 key购买 nike

<分区>

在尝试将我的 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() );

public class StandingsAdapter extends ArrayAdapter {
List list = new ArrayList();
public StandingsAdapter(@NonNull Context context, @LayoutRes int resource) {
super( context, resource );
}

public void add(Standings object) {
super.add( object );
list.add( object );
}

@Override
public int getCount() {
return list.size();
}

@Nullable
@Override
public Object getItem(int position) {
return list.get( position );
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row;
row = convertView;
StandingsHolder standingsHolder;
if(row ==null){
LayoutInflater layoutInflater= (LayoutInflater) this.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
row = layoutInflater.inflate(R.layout.row_layout, parent, false);
standingsHolder = new StandingsHolder();
standingsHolder.tx_id = (TextView) row.findViewById( R.id.sec_1 );
standingsHolder.txt_id = (TextView) row.findViewById( R.id.sec_2 );
standingsHolder.tx_fn = (TextView) row.findViewById( R.id.sec_3 );
standingsHolder.tx_ln = (TextView) row.findViewById( R.id.sec_4 );
standingsHolder.tx_pos = (TextView) row.findViewById( R.id.sec_5 );
standingsHolder.tx_po = (TextView) row.findViewById( R.id.sec_6 );
row.setTag( standingsHolder );
}
else
{
standingsHolder = (StandingsHolder) row.getTag();
}
Standings standings = (Standings)this.getItem( position );
standingsHolder.tx_id.setText( standings.getDriver_id() );
standingsHolder.txt_id.setText( standings.getTeam_id() );
standingsHolder.tx_fn.setText( standings.getFirstName() );
standingsHolder.tx_ln.setText( standings.getLastName() );
standingsHolder.tx_pos.setText( standings.getPosition() );
standingsHolder.tx_po.setText( standings.getPoints() );

return row;
}

static class StandingsHolder {
TextView tx_id, txt_id, tx_fn, tx_ln, tx_pos, tx_po;
}
}

我将发布我的 android 应用程序中涉及此错误的其他类。下面是我的 Standings 类

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 fragment ,它是 Main Fragment,其中包含用于下载 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;

}


}
}

我拥有的最后一个 classJSON Parse 一起工作的是 Display in List View 类,这意味着它的名字是什么。下面是:

public class DisplayListView extends AppCompatActivity {

String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
StandingsAdapter standingsAdapter;
ListView listView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.display_list_view_layout );
listView= (ListView) findViewById( R.id.ListViewParse );
standingsAdapter = new StandingsAdapter(this,R.layout.row_layout);
listView.setAdapter( standingsAdapter );
json_string = 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();
}

}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com