Java Android Studio解析JSON对象

Java Android Studio解析JSON对象,java,android,json,Java,Android,Json,我正在开发一个解析json并从我的服务器检索数据的程序(它是json对象而不是数组)。我的问题是我的应用程序在需要解析json时崩溃;我不明白我的错误。我会给你看我的节目。我通常使用三个类来解析JSON: 在我的布局上添加谁是我的列表视图(显示视图) 满足于吃苦耐劳的人 在版面(新版面)的不同文本视图中写入数据的valeursAdapater 我点击一个按钮,在我的主要活动中解析我的json public void parseJSON(View view) { Inten

我正在开发一个解析json并从我的服务器检索数据的程序(它是json对象而不是数组)。我的问题是我的应用程序在需要解析json时崩溃;我不明白我的错误。我会给你看我的节目。我通常使用三个类来解析JSON:

  • 在我的布局上添加谁是我的列表视图(显示视图)

  • 满足于吃苦耐劳的人

  • 在版面(新版面)的不同文本视图中写入数据的valeursAdapater

我点击一个按钮,在我的主要活动中解析我的json

public void parseJSON(View view) {


        Intent intent=new Intent(this,AfficheurListView.class);
        intent.putExtra("json_data",json_string);
        startActivity(intent);
    }
AfficheurListView.java

public class AfficheurListView extends AppCompatActivity {
String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
JSONObject jObj = null;
ValeursAdapter valeursAdapter;

ListView listView;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_listview_layout);
    listView =(ListView)findViewById(R.id.listview);
    valeursAdapter=new ValeursAdapter(this,R.layout.new_layout);
    listView.setAdapter(valeursAdapter);
    json_string=getIntent().getExtras().getString("json_data");

    try {
      // jsonObject=new JSONObject(json_string);
        //    jsonArray=jsonObject.getJSONArray("server_reponse");
       // int count=0 ;

        String mpx,rds,al,ar,frequence,pilots,id,id_SIGFOX,timestamps,rf;
        jObj = new JSONObject(json_string);
        mpx=  jObj.getString("MPX");
        rds =jObj.getString("RDS");
        rf=jObj.getString("RF");
        frequence =jObj.getString("Frequence");
        timestamps=jObj.getString("timestamp");
        id= jObj.getString("id");
        id_SIGFOX= jObj.getString("id_SIGFOX");
        pilots= jObj.getString("PILOT");
        al= jObj.getString("a_l");
        ar= jObj.getString("a_r");

            Valeurs valeurs=new Valeurs(mpx,rds,al,ar,frequence,pilots,id,timestamps,id_SIGFOX,rf);
            valeursAdapter.add(valeurs);
    } catch (JSONException e) {

        e.printStackTrace();

    }

}
}
日志在这里

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.suprem.myapplication, PID: 3835
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:4780)
                  at android.view.View$PerformClick.run(View.java:19866)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:4780) 
                  at android.view.View$PerformClick.run(View.java:19866) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
               Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.suprem.myapplication/com.suprem.myapplication.AfficheurListView}; have you declared this activity in your AndroidManifest.xml?
                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
                  at android.app.Activity.startActivityForResult(Activity.java:3745)
                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                  at android.app.Activity.startActivityForResult(Activity.java:3706)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                  at android.app.Activity.startActivity(Activity.java:4016)
                  at android.app.Activity.startActivity(Activity.java:3984)
                  at com.suprem.myapplication.MainActivity.parseJSON(MainActivity.java:130)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:4780) 
                  at android.view.View$PerformClick.run(View.java:19866) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
java

public class Valeurs {
private String mpx,rds,al,ar,pilots,frequence,id,timestamps,id_SIGFOX,rf;
        public Valeurs(String mpx, String rds, String al, String ar, String pilots, String frequence, String id, String timestamps, String id_SIGFOX, String rf)

        {
            this.setMpx(mpx);
            this.setRds(rds);
            this.setAl(al);
            this.setAr(ar);
            this.setPilots(pilots);
            this.setFrequence(frequence);
            this.setId(id);
            this.setTimestamps(timestamps);
            this.setId_SIGFOX(id_SIGFOX);
            this.setRf(rf);


        }


    public String getMpx() {
        return mpx;
    }

    public void setMpx(String mpx) {
        this.mpx = mpx;
    }

    public String getRds() {
        return rds;
    }

    public void setRds(String rds) {
        this.rds = rds;
    }

    public String getAl() {
        return al;
    }

    public void setAl(String al) {
        this.al = al;
    }

    public String getAr() {
        return ar;
    }

    public void setAr(String ar) {
        this.ar = ar;
    }

    public String getPilots() {
        return pilots;
    }

    public void setPilots(String pilots) {
        this.pilots = pilots;
    }

    public String getFrequence() {
        return frequence;
    }

    public void setFrequence(String frequence) {
        this.frequence = frequence;
    }

    public String getTimestamps() {
        return timestamps;
    }

    public void setTimestamps(String timestamps) {
        this.timestamps = timestamps;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId_SIGFOX() {
        return id_SIGFOX;
    }

    public void setId_SIGFOX(String id_SIGFOX) {
        this.id_SIGFOX = id_SIGFOX;
    }

    public String getRf() {
        return rf;
    }

    public void setRf(String rf) {
        this.rf = rf;
    }
}
ValeursAdapter.java

public class ValeursAdapter  extends ArrayAdapter{

    List list=new ArrayList<>();
    public ValeursAdapter(Context context, int resource) {
        super(context, resource);
    }


    public void add(Valeurs 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, View convertView, ViewGroup parent) {
         View row;
        row=convertView;

        ValeursDefini valeursDefini;
        if(row==null)
        {
            LayoutInflater layoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=layoutInflater.inflate(R.layout.new_layout,parent,false);
            valeursDefini=new ValeursDefini();
            valeursDefini.tx_mpx=(TextView)row.findViewById(R.id.tx_mpx);
            valeursDefini.tx_al=(TextView)row.findViewById(R.id.tx_al);
            valeursDefini.tx_ar=(TextView)row.findViewById(R.id.tx_ar);
            valeursDefini.tx_rds=(TextView)row.findViewById(R.id.tx_rds);
            valeursDefini.tx_frequence=(TextView)row.findViewById(R.id.tx_frequence);
            valeursDefini.tx_pilots=(TextView)row.findViewById(R.id.tx_pilots);

            row.setTag(valeursDefini);

        }
            else {
        valeursDefini=(ValeursDefini)row.getTag();
        }
        Valeurs valeurs= (Valeurs) this.getItem(position);
        valeursDefini.tx_mpx.setText(valeurs.getMpx());
        valeursDefini.tx_rds.setText(valeurs.getRds());
        valeursDefini.tx_al.setText(valeurs.getAl());
        valeursDefini.tx_ar.setText(valeurs.getAr());
        valeursDefini.tx_pilots.setText(valeurs.getPilots());
        valeursDefini.tx_frequence.setText(valeurs.getFrequence());

        return row;
    }
    static class ValeursDefini
    {
        TextView tx_mpx,tx_rds,tx_al,tx_ar,tx_frequence,tx_pilots;

    }
}
公共类ValeurAdapter扩展了ArrayAdapter{
列表=新的ArrayList();
公共适配器(上下文,int资源){
超级(上下文、资源);
}
公共无效添加(Valeurs对象){
super.add(object);
列表。添加(对象);
}
@凌驾
public int getCount(){
返回list.size();
}
@可空
@凌驾
公共对象getItem(int位置){
返回列表。获取(位置);
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
查看行;
行=转换视图;
瓦勒斯迪尼瓦勒斯迪尼;
if(行==null)
{
LayoutInflater LayoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
行=布局更平坦。充气(R布局。新布局,父级,false);
ValeurDefiniti=新的ValeurDefiniti();
ValeurDefiniti.tx\u mpx=(TextView)row.findViewById(R.id.tx\u mpx);
ValeurDefiniti.tx_al=(TextView)row.findViewById(R.id.tx_al);
ValerDefiniti.tx_ar=(TextView)row.findViewById(R.id.tx_ar);
valerDefiniti.tx\u rds=(TextView)row.findViewById(R.id.tx\u rds);
valerDefiniti.tx\u frequency=(TextView)row.findViewById(R.id.tx\u frequency);
ValeurDefiniti.tx_pilots=(TextView)row.findViewById(R.id.tx_pilots);
行。设置标签(valeursDefini);
}
否则{
valeursDefini=(valeursDefini)row.getTag();
}
Valeurs Valeurs=(Valeurs)此.getItem(位置);
valeursDefini.tx_mpx.setText(valeurs.getMpx());
valeursDefini.tx_rds.setText(valeurs.getRds());
valeursDefini.tx_al.setText(valeurs.getAl());
valeursDefini.tx_ar.setText(valeurs.getAr());
valeursDefini.tx_pilots.setText(valeurs.getPilots());
valeursDefini.tx_frequence.setText(valeurs.getFrequence());
返回行;
}
静态类ValeursDefini
{
text查看发送mpx、发送rds、发送al、发送ar、发送频率、发送导频;
}
}

您尝试过stacktrace的建议吗?原因:android.content.ActivityNotFoundException:找不到显式活动类{com.suprem.myapplication/com.suprem.myapplication.AfficheurListView};你在你的AndroidManifest.xml中声明了这个活动了吗?哟,我的那个错误是的,就是那个!!我的ex类的名称为DisplayListView,我不编辑清单!!!谢谢你的回答