ボタンをクリックすると、このメッセージがエミュレーターディスプレイに表示されます
ラジオ アプリケーションに問題があります。エミュレータでアプリケーションを実行すると、ボタンをクリックすると、アプリケーションは次のように言います。
アプリケーション 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;
}
}