In this given code, my HTTP-related code is working correctly and it is giving correct result in toast message. However when I apply parsing of the HTTP content (XML), it will force close.
What is the exact problem with what I'm doing here?
public class Player extends Activity {
String d="";
TextView id[];
TextView name[];
String str;
TableLayout t1;
private HttpEntity responseEntity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
TableLayout t1 = (TableLayout) findViewById(R.id.myTable);
Intent i = getIntent();
Bundle b = i.getExtras();
String str = b.getString("ARRIVING_FROM");
TextView title = (TextView) findViewById(R.id.TextView01);
title.setText(str);
d=d+"";
d = tryLogin(str);
System.out.println("Value of D"+d.substring(0, 1));
if(d.substring(0, 1).equalsIgnoreCase("1"))
//if(d.equals(str))
{
Toast.makeText(getApplicationContext(), "OOPS", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getApplicationContext(), d, Toast.LENGTH_SHORT).show();
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XMLHandler myXMLHandler = new XMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(retrieveInputStream(responseEntity));
} catch (Exception e) {
System.out.println("XML Parsing Excpetion = " + e);
}
final ScoreList scorelist = XMLHandler.scorelist ;
id = new TextView[scorelist.getName().size()];
name = new TextView[scorelist.getName().size()];
for (int i1 = 0; i1 < scorelist.getName().size(); i1++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
id[i1]= new TextView(this);
id[i1].setText(scorelist.getId().get(i1));
id[i1].setTextColor(Color.WHITE);
id[i1].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.addView(id[i1]);
name[i1]= new TextView(this);
name[i1].setText(scorelist.getName().get(i1));
name[i1].setTextColor(Color.WHITE);
name[i1].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.addView(name[i1]);
t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
}
private InputSource retrieveInputStream(HttpEntity responseEntity) {
// TODO Auto-generated method stub
return null;
}
protected String tryLogin(String str)
{
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ip_address/test/player_details.php?player_id="+str.replace(' ', '+'));
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("player_id", str));
try
{
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
Log.v("MyPlayerInfo", response.getStatusLine().toString());
HttpEntity responseEntity = response.getEntity();
InputStream in=responseEntity.getContent();
byte[] bData = new byte[1024];
in.read(bData);
System.out.println("In Data"+in.toString());
String st=new String (bData);
d =st;
System.out.println("Response String from server"+st);
Log.v("MyPlayerInfo", "Set response to responseEntity");
return d;
} catch(Exception e)
{
Log.i("Catch","Exception generate in Post");
e.printStackTrace();
}return"0";
}
}
logcat response:
yes this is my logcat response:
03-29 17:01:24.028: DEBUG/AndroidRuntime(2300): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
0开发者_开发百科3-29 17:01:24.028: DEBUG/AndroidRuntime(2300): CheckJNI is ON
03-29 17:01:24.218: DEBUG/AndroidRuntime(2300): --- registering native functions ---
03-29 17:01:24.988: DEBUG/AndroidRuntime(2300): Shutting down VM
03-29 17:01:24.998: DEBUG/dalvikvm(2300): Debugger has detached; object registry had 1 entries
03-29 17:01:25.028: INFO/AndroidRuntime(2300): NOTE: attach of thread 'Binder Thread #3' failed
03-29 17:01:36.128: INFO/System.out(2314): Response String from server1ÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ
03-29 17:01:36.128: VERBOSE/MyParsingExample(2314): Set response to responseEntity
03-29 17:01:36.128: INFO/System.out(2314): Value of D1
03-29 17:01:36.138: INFO/ActivityManager(43): Starting activity: Intent { cmp=android.example/.MyPlayerInfo (has extras) }
03-29 17:01:36.408: DEBUG/dalvikvm(2314): GC_FOR_MALLOC freed 3151 objects / 182456 bytes in 85ms
03-29 17:01:36.508: VERBOSE/MyPlayerInfo(2314): HTTP/1.1 200 OK
03-29 17:01:36.508: INFO/System.out(2314): In Dataorg.apache.http.conn.EofSensorInputStream@43eaac68
03-29 17:01:36.528: INFO/System.out(2314): Response String from server<result>
03-29 17:01:36.528: INFO/System.out(2314): <playerid>1</player_id>
03-29 17:01:36.528: INFO/System.out(2314): <player_name>Sachin Tendulkar</player_name>
03-29 17:01:36.538: INFO/System.out(2314): </result>ÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ
03-29 17:01:36.538: VERBOSE/MyPlayerInfo(2314): Set response to responseEntity
03-29 17:01:36.538: INFO/System.out(2314): Value of D<
03-29 17:01:36.568: INFO/System.out(2314): XML Parsing Excpetion = java.lang.NullPointerException
03-29 17:01:36.578: DEBUG/AndroidRuntime(2314): Shutting down VM
03-29 17:01:36.578: WARN/dalvikvm(2314): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): FATAL EXCEPTION: main
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.example/android.example.MyPlayerInfo}: java.lang.NullPointerException
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.os.Looper.loop(Looper.java:123)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at java.lang.reflect.Method.invoke(Method.java:521)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at dalvik.system.NativeStart.main(Native Method)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): Caused by: java.lang.NullPointerException
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.example.MyPlayerInfo.onCreate(MyPlayerInfo.java:91)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 17:01:36.598: ERROR/AndroidRuntime(2314): ... 11 more
03-29 17:01:36.618: WARN/ActivityManager(43): Force finishing activity android.example/.MyPlayerInfo
03-29 17:01:36.628: WARN/ActivityManager(43): Force finishing activity android.example/.MyParsingExample
03-29 17:01:37.174: WARN/ActivityManager(43): Activity pause timeout for HistoryRecord{44051d20 android.example/.MyPlayerInfo}
03-29 17:01:38.508: INFO/Process(2314): Sending signal. PID: 2314 SIG: 9
03-29 17:01:38.538: INFO/ActivityManager(43): Process android.example (pid 2314) has died.
03-29 17:01:38.538: INFO/WindowManager(43): WIN DEATH: Window{440299d8 android.example/android.example.MyParsingExample paused=true}
03-29 17:01:38.578: WARN/NotificationService(43): Object died trying to hide notification android.app.ITransientNotification$Stub$Proxy@440bf590 in package android.example
03-29 17:01:38.578: WARN/ActivityManager(43): setProcessForeground called on unknown pid: 2314
03-29 17:01:38.688: WARN/InputManagerService(43): Got RemoteException sending setActive(false) notification to pid 2314 uid 10074
03-29 17:01:47.592: WARN/ActivityManager(43): Activity destroy timeout for HistoryRecord{43f68630 android.example/.MyParsingExample}
03-29 17:01:47.592: WARN/ActivityManager(43): Activity destroy timeout for HistoryRecord{44051d20 android.example/.MyPlayerInfo}
You have a NullPointerException
on line 91 of MyPlayerInfo
, in the onCreate()
method.
Also, never do network I/O and parsing on the main application thread. Use an AsyncTask
or some other form of background thread, please.
精彩评论