致命异常:主要(抱歉应用程序无线电已意外停止)OnButtonClick

时间:2012-07-27 11:39:35

标签: android android-emulator fatal-error

当我点击按钮时,此消息显示在模拟器显示

我的Radio应用程序出现问题。当我在Emulator中运行应用程序时,我点击按钮然后应用程序说:

很抱歉应用程序Radio(进程org.example.radio)意外停止了。请再试一次。

过去它还没有这样做,我很困惑从哪里开始。我看了看logcat,但我不确定我应该寻找什么,以及如何解决它。这是我的代码

- this is my code


package com.example.radio;

    import java.sql.ResultSet;
    import java.sql.SQLException;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button) findViewById(R.id.button1);
        final TextView Txt = (TextView) findViewById(R.id.textView1);

        b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                WebService webs = new WebService();
                webs.response();
                ResultSet st = webs.ResultReturn();
                try
                {
                    while(st.next())
                    {
                        Txt.setText(st.getString("uname"));
                    }
                } catch (Exception e) {
                    Txt.setText(e.getMessage());
                }

            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

- 这是LogCat上的错误

07-26 20:14:07.116: I/ActivityManager(61): Start proc com.example.radio for activity com.example.radio/.MainActivity: pid=1117 uid=10034 gids={3003}
07-26 20:14:08.485: I/ActivityManager(61): Displayed com.example.radio/.MainActivity: +3s330ms (total +36s5ms)
07-26 20:14:13.705: D/dalvikvm(697): GC_EXPLICIT freed 99K, 49% free 2980K/5831K, external 5894K/7299K, paused 177ms
07-26 20:14:25.165: D/AndroidRuntime(1117): Shutting down VM
07-26 20:14:25.165: W/dalvikvm(1117): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-26 20:14:25.215: E/AndroidRuntime(1117): FATAL EXCEPTION: main
07-26 20:14:25.215: E/AndroidRuntime(1117): java.lang.ClassCastException: org.json.JSONObject
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.example.radio.WebService.JsonData(WebService.java:111)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.example.radio.WebService.response(WebService.java:54)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.example.radio.MainActivity$1.onClick(MainActivity.java:31)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.view.View.performClick(View.java:2485)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.view.View$PerformClick.run(View.java:9080)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.os.Handler.handleCallback(Handler.java:587)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.os.Looper.loop(Looper.java:123)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invoke(Method.java:507)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-26 20:14:25.215: E/AndroidRuntime(1117):     at dalvik.system.NativeStart.main(Native Method)
07-26 20:14:25.255: W/ActivityManager(61):   Force finishing activity com.example.radio/.MainActivity
07-26 20:14:25.815: W/ActivityManager(61): Activity pause timeout for HistoryRecord{406a26e0 com.example.radio/.MainActivity}
07-26 20:14:29.835: I/InputDispatcher(61): Application is not responding: Window{407a49e0 com.example.radio/com.example.radio.MainActivity paused=false}.  5010.9ms since event, 5009.8ms since wait started
07-26 20:14:29.835: I/WindowManager(61): Input event dispatching timed out sending to com.example.radio/com.example.radio.MainActivity
07-26 20:14:29.835: I/InputDispatcher(61): Dropping event because the pointer is not down.
07-26 20:14:37.755: W/ActivityManager(61): Activity destroy timeout for HistoryRecord{406a26e0 com.example.radio/.MainActivity}

这是WebService类

package com.example.radio;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.TextView;

public class WebService {

    private String _result = null;
    private InputStream _is = null;
    private ResultSet st = null;
    private String HttpUrl = "http://bolanhost.com/livestream/WebService.php";



    //constructor
    public WebService() {

    }
    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    public void response()
    {
        this.httpPost("http://bolanhost.com/livestream/WebService.php");
        this.Bufferreader();
        this.JsonData();
    }


    private void httpPost(String _HttpUrl)
    {
        nameValuePairs.add(new BasicNameValuePair("uname","Hello"));
        //httpPost
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(_HttpUrl);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            _is = entity.getContent();
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }
    }


    private void Bufferreader()
    {

        //convert response to string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(_is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            _is.close();

            _result=sb.toString();
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }
    }

    private void JsonData()
    {
        //parse json data
        try
        {
            JSONArray jArray = new JSONArray(_result);
            for(int i=0;i<jArray.length();i++){
               st = (ResultSet) jArray.getJSONObject(i);

        }


        }
        catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }

    }


    public ResultSet ResultReturn()
    {
        return st;
    }


}

3 个答案:

答案 0 :(得分:3)

使用JSONObject,Dont use ResultSet

因为JSONArray have JSONObject's Array。你正在尝试使用ResultSet.So这两个类不一样,,,所以ClassCastException。

 try{

   JSONArray jArray = new JSONArray(_result);

   for(int i=0;i<jArray.length();i++){

          JSONObject objJson = jArray.getJSONObject(i);
          String strName = objJson.getString("uname"); 

          Log.i("Name is "+strName); 

          }

        }

答案 1 :(得分:1)

查看堆栈跟踪 - 问题出在第111行。可能是你投射的地方

st = (ResultSet) jArray.getJSONObject(i);

getJSONObject(i)返回JSONObject而不是ResultSet。检查JSON文档。

另一件事是你不应该在主线程上使用Web服务。尝试使用AsyncTask进行网络访问。

答案 2 :(得分:0)

请检查您的网络服务响应,因为您的错误显示为ClassCastException。