0

我是android的新手,我下载了一个源代码,但它没有运行任何人都可以帮助我。没有我的交叉签名,但我的程序也没有运行,我无法知道 logcat 的错误输出这是我的主要活动代码

public class MainActivity extends Activity {

/** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       TextView JsonSrc = (TextView)findViewById(R.id.json_src);
       TextView JsonResult = (TextView)findViewById(R.id.json_result);
       /*
        * JSON:
        * {"id":12,"name":12,"status":"ok","volumes":[{"id":17592,"name":"root","status":"ok"}]}
        */
       final String customJSON = "{\"id\":12,\"name\":12,\"status\":\"ok\",\"volumes\":[{\"id\":17592,\"name\":\"root\",\"status\":\"ok\"}]}";
       JsonSrc.setText(customJSON);
       try {
   JSONObject jsonObject = new JSONObject(customJSON);
   String myId = jsonObject.getString("id");
   String myName = jsonObject.getString("name");
   String myStatus = jsonObject.getString("status");
   String stringJsonResult = "id: " + myId + "\n"
        + "name: " + myName + "\n"
        + "status: " + myStatus;


   JSONArray jsonArray = jsonObject.getJSONArray("volumes");
   JSONObject arrayElement_0 = jsonArray.getJSONObject(0);
   String ele0_id = arrayElement_0.getString("id");
   String ele0_name = arrayElement_0.getString("name");
   String ele0_status = arrayElement_0.getString("status");
   stringJsonResult += "\n\nArray Element 0 - \n"
        + "id: " + ele0_id + "\n"
        + "name: " + ele0_name + "\n"
        + "status: " + ele0_status;

   JsonResult.setText("\n" + stringJsonResult);

  } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
  }
   }
}

这是我的 logcat 输出

08-24 10:01:56.662: E/AndroidRuntime(798): java.lang.RuntimeException: Unable to start  activityComponentInfo{com.mokshya.androidparse/com.mokshya.androidparse.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup

请帮我。

4

1 回答 1

2

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup

这应该给你一些开始调查的东西。

此错误意味着某处类型的对象TextView被强制转换为 type ViewGroup。从您发布的代码中,我看不到任何此类问题。可能是您的activity_main.xml. 我希望你的根标签是 *Layout 类型的。

于 2012-10-02T10:03:32.127 回答