我在尝试访问Enterdata()中的textview时遇到空指针异常; *该网站正在运行并返回一个jsonobject *我在activity_main.xml中创建了id通知 *我能够在oncreate方法中编辑和访问它 请帮忙
package com.example.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
int success = 56;
// String requestUrl = "http://hitesh.comeze.com/minor/index.phphttp://hitesh.comeze.com/minor/index.php";
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
new jsop().execute("");
}
public void Enterdata(JSONObject jsonob) {
try {
View myTestView = getLayoutInflater().inflate(R.layout.activity_main, null);
TextView notice = (TextView) myTestView.findViewById(R.id.notice);
success = jsonob.getInt("success");
Log.d("hi", " "+success);
System.out.print(success);
notice.append(" "+success+" ");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
class jsop extends AsyncTask<String,String,JSONObject>
{
static InputStream is = null;
static JSONObject jObj = null;
static String json="";
@Override
protected JSONObject doInBackground(String... params) {
return getJSONFromUrl("http://hitesh.comeze.com/minor/index.php");
}
protected void onPostExecute(JSONObject jsonob) {
new MainActivity().Enterdata(jsonob);
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse!=null)
{HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else
{
System.out.print("htttpresponse");
}
}
catch (Exception e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
if(reader!=null)
{
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
这是日志文件
04-13 08:33:03.046: D/gralloc_goldfish(1917): Emulator without GPU emulation detected.
04-13 08:33:04.506: D/AndroidRuntime(1917): Shutting down VM
04-13 08:33:04.506: W/dalvikvm(1917): threadid=1: thread exiting with uncaught exception (group=0xb2a75ba8)
04-13 08:33:04.536: E/AndroidRuntime(1917): FATAL EXCEPTION: main
04-13 08:33:04.536: E/AndroidRuntime(1917): Process: com.example.test, PID: 1917
04-13 08:33:04.536: E/AndroidRuntime(1917): java.lang.NullPointerException
04-13 08:33:04.536: E/AndroidRuntime(1917): at android.app.Activity.getLayoutInflater(Activity.java:3338)
04-13 08:33:04.536: E/AndroidRuntime(1917): at com.example.test.MainActivity.Enterdata(MainActivity.java:39)
04-13 08:33:04.536: E/AndroidRuntime(1917): at com.example.test.jsop.onPostExecute(MainActivity.java:60)
04-13 08:33:04.536: E/AndroidRuntime(1917): at com.example.test.jsop.onPostExecute(MainActivity.java:1)
04-13 08:33:04.536: E/AndroidRuntime(1917): at android.os.AsyncTask.finish(AsyncTask.java:632)
04-13 08:33:04.536: E/AndroidRuntime(1917): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-13 08:33:04.536: E/AndroidRuntime(1917): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-13 08:33:04.536: E/AndroidRuntime(1917): at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 08:33:04.536: E/AndroidRuntime(1917): at android.os.Looper.loop(Looper.java:136)
04-13 08:33:04.536: E/AndroidRuntime(1917): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-13 08:33:04.536: E/AndroidRuntime(1917): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 08:33:04.536: E/AndroidRuntime(1917): at java.lang.reflect.Method.invoke(Method.java:515)
04-13 08:33:04.536: E/AndroidRuntime(1917): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-13 08:33:04.536: E/AndroidRuntime(1917): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-13 08:33:04.536: E/AndroidRuntime(1917): at dalvik.system.NativeStart.main(Native Method)
04-13 08:38:04.626: I/Process(1917): Sending signal. PID: 1917 SIG: 9
应用hariharan的建议后的代码和日志(顺便说一下,这样快速的反应)
package com.example.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
int success = 56;
TextView notice;
// String requestUrl = "http://hitesh.comeze.com/minor/index.phphttp://hitesh.comeze.com/minor/index.php";
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
notice = (TextView) findViewById(R.id.notice);
new jsop().execute("");
}
public void Enterdata(JSONObject jsonob) {
try {
success = jsonob.getInt("success");
notice.setText(" "+success+" ");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
class jsop extends AsyncTask<String,String,JSONObject>
{
static InputStream is = null;
static JSONObject jObj = null;
static String json="";
@Override
protected JSONObject doInBackground(String... params) {
return getJSONFromUrl("http://hitesh.comeze.com/minor/index.php");
}
protected void onPostExecute(JSONObject jsonob) {
new MainActivity().Enterdata(jsonob);
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse!=null)
{HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else
{
System.out.print("htttpresponse");
}
}
catch (Exception e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
if(reader!=null)
{
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
logcat的
04-13 09:04:58.086: D/gralloc_goldfish(2148): Emulator without GPU emulation detected.
04-13 09:04:59.626: D/AndroidRuntime(2148): Shutting down VM
04-13 09:04:59.626: W/dalvikvm(2148): threadid=1: thread exiting with uncaught exception (group=0xb2a75ba8)
04-13 09:04:59.656: E/AndroidRuntime(2148): FATAL EXCEPTION: main
04-13 09:04:59.656: E/AndroidRuntime(2148): Process: com.example.test, PID: 2148
04-13 09:04:59.656: E/AndroidRuntime(2148): java.lang.NullPointerException
04-13 09:04:59.656: E/AndroidRuntime(2148): at com.example.test.MainActivity.Enterdata(MainActivity.java:41)
04-13 09:04:59.656: E/AndroidRuntime(2148): at com.example.test.jsop.onPostExecute(MainActivity.java:57)
04-13 09:04:59.656: E/AndroidRuntime(2148): at com.example.test.jsop.onPostExecute(MainActivity.java:1)
04-13 09:04:59.656: E/AndroidRuntime(2148): at android.os.AsyncTask.finish(AsyncTask.java:632)
04-13 09:04:59.656: E/AndroidRuntime(2148): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-13 09:04:59.656: E/AndroidRuntime(2148): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-13 09:04:59.656: E/AndroidRuntime(2148): at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 09:04:59.656: E/AndroidRuntime(2148): at android.os.Looper.loop(Looper.java:136)
04-13 09:04:59.656: E/AndroidRuntime(2148): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-13 09:04:59.656: E/AndroidRuntime(2148): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 09:04:59.656: E/AndroidRuntime(2148): at java.lang.reflect.Method.invoke(Method.java:515)
04-13 09:04:59.656: E/AndroidRuntime(2148): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-13 09:04:59.656: E/AndroidRuntime(2148): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-13 09:04:59.656: E/AndroidRuntime(2148): at dalvik.system.NativeStart.main(Native Method)
04-13 09:09:59.796: I/Process(2148): Sending signal. PID: 2148 SIG: 9
答案 0 :(得分:1)
试试这个..
使用notice.setText(" "+success+" ");
TextView notice;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
notice = (TextView) findViewById(R.id.notice);
new jsop().execute("");
}
public void Enterdata(JSONObject jsonob) {
try {
success = jsonob.getInt("success");
Log.d("hi", " "+success);
System.out.print(success);
notice.setText(" "+success+" ");
} catch (JSONException e) {
e.printStackTrace();
}
}
修改强>
protected void onPostExecute(JSONObject jsonob) {
Enterdata(jsonob);
}
修改强>
package com.example.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
int success = 56;
TextView notice;
// String requestUrl = "http://hitesh.comeze.com/minor/index.phphttp://hitesh.comeze.com/minor/index.php";
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
notice = (TextView) findViewById(R.id.notice);
new jsop().execute("");
}
public void Enterdata(JSONObject jsonob) {
try {
success = jsonob.getInt("success");
notice.setText(" "+success+" ");
} catch (JSONException e) {
e.printStackTrace();
}
}
class jsop extends AsyncTask<String,String,JSONObject>
{
static InputStream is = null;
static JSONObject jObj = null;
static String json="";
@Override
protected JSONObject doInBackground(String... params) {
return getJSONFromUrl("http://hitesh.comeze.com/minor/index.php");
}
protected void onPostExecute(JSONObject jsonob) {
new MainActivity().Enterdata(jsonob);
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse!=null)
{HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else
{
System.out.print("htttpresponse");
}
}
catch (Exception e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
if(reader!=null)
{
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
}
答案 1 :(得分:0)
尝试在Enterdata(JSONObject jsonob) : 39
myTextView = ((LayoutInflater) MainActivity.this.getSystemService("layout_inflater"))
.inflate(R.layout.activity_main, null);