当前位置: 首页 > 知识库问答 >
问题:

当我试图将我的JSON解析为列表视图时,我收到了一个资源未找到的异常?[副本]

慕健
2023-03-14

当我试图将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;
    }
}
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;
    }
}
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;

                }


            }
        }
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();
        }

    }
}

共有1个答案

赵鸿畴
2023-03-14

您需要使用Integer.ToString(Intargument)将int值转换为String;

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()));
 类似资料:
  • 我创造了一个activity,在这个activity里,我有不同的碎片。在一个片段中,我想检查用户是否已登录,如果已登录显示一个布局,如果未登录显示另一个布局。 这是我做的,你能过来看看吗? 这是我得到的错误 E/AndroidRuntime:致命异常:主进程:#########,PID:562 Android.content.res.resourcesimpl.getValue(resource

  • 我以为我了解Docker。我把它理解为一种打包软件的方法,其中包含很多依赖项。。基本上创建一个小世界,在这个小世界里,软件的每一件事情都会得到处理。然后我在DockerHub上偶然发现了这个 https://hub.docker.com/_/busybox/ 这是BusyBox的一个映像,它是一个用于嵌入式系统的小型Linux二进制文件。然后上面的评论说: Busybox是真棒:)到目前为止,在整

  • 当我尝试编译项目时,我收到了这个错误: java版本 OpenJDK版本“11.0.2”2019-01-15 OpenJDK运行时环境(build 11.0.2 9-Debian-3)OpenJDK 64位服务器VM(build 11.0.2 9-Debian-3,混合模式,共享) mvn-版本 Maven home:/usr/share/Maven Java版本:11.0.2,供应商:Oracl

  • 问题内容: 请问为什么第13行中的错误是未报告的异常,必须在声明声明为pr的情况下将其捕获 问题答案: 您需要向引发异常的方法中添加一个,如上所述,以及调用该方法的所有方法

  • 我正在尝试将从数据库接收到的列表对象转换为Json,但当我尝试转换时,我得到了。 这是我的代码: 想法是,我从数据库中检索一个Orderobjects列表,将其传递给dataTableResult,并将其发送到前端由Jquerydatatable显示,我得到了这些列表,但是当我尝试使用GSON()来尝试转换orederList或dataTableResult时,它会抛出一个错误。我切换到flexJ

  • 问题内容: 我对laravel视图有问题,找不到路由函数,我做了作曲家dumpautoload但没有使用ArticleController.php InvalidArgumentException 问题答案: 当Laravel在您的应用程序中找不到视图文件时,就会发生这种情况。确保你有一个文件名为:或者你在目录中。 请注意,在调用时Laravel将执行以下操作: 对于Laravel,它将查找文件: