我有活动和课程。在活动中我做HTTP GET请求(HttpClass.java),然后在GetMethod.java中我做Asynctask。在onPostExecute函数中,我想返回活动并使用JSON解析数据。它可以工作,但当我尝试使用textview和按钮programmaticaly为数据数组的每个项创建布局时,它会抛出NullPointerException。
代码:
HttpClass.java
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.httplayout);
cont = this;
ll = (LinearLayout) findViewById(R.id.llHttp);
sv = (ScrollView) findViewById(R.id.svHttp);
//httptv = (TextView) findViewById(R.id.tvHttp);
font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
font2 = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
GetMethod test = new GetMethod();
test.execute(new String[] {"url"});
}
public void getResultData(String result) throws Exception {
// TODO Auto-generated method stub
resultdata = result;
JSONObject jObject;
jObject = new JSONObject(resultdata);
//JSONObject object = jObject.getJSONObject("pharms");
JSONArray jArray = jObject.getJSONArray("pharms");
for (int i = 0; i < jArray.length(); i++) {
name = jArray.getJSONObject(i).getString("name").toString();
phone = jArray.getJSONObject(i).getString("phone").toString();
latitude = jArray.getJSONObject(i).getString("latitude").toString();
longitude = jArray.getJSONObject(i).getString("longitude").toString();
address = jArray.getJSONObject(i).getString("address").toString();
RelativeLayout ll2 = new RelativeLayout(cont);
//ll2.setOrientation(Orientation.ho)
Button call = new Button(cont);
call.setId(i+1);
call.setClickable(true);
call.setTypeface(font);
call.setText(R.string.call);
call.setTextColor(Color.rgb(0, 176, 128));
call.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)call.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params1.setMargins(0, 25, 0, 0);
call.setLayoutParams(params1);
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
}
});
ll2.addView(call);
TextView tv = new TextView(cont);
tv.setId(i+1);
tv.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams)tv.getLayoutParams();
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params3.setMargins(10, 25, 0, 0);
tv.setLayoutParams(params3);
tv.setText(name + "\n" + phone);
tv.setTextSize(20);
tv.setTypeface(font2);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
ll2.addView(tv);
ll.addView(ll2);
}
}
这是GetMethod.java类
@Override
protected String doInBackground(String... urls){
BufferedReader in = null;
String data = null;
for (String url : urls) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} catch (Exception e) {
e.printStackTrace();
}
}
return data;
}
@Override
protected void onPostExecute(String result) {
HttpClass hc = new HttpClass();
try {
hc.getResultData(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
编辑:LogCat日志
09-17 01:35:53.955: W/System.err(18700): java.lang.NullPointerException
09-17 01:35:53.980: W/System.err(18700): at android.view.ViewConfiguration.get(ViewConfiguration.java:332)
09-17 01:35:53.980: W/System.err(18700): at android.view.View.<init>(View.java:3322)
09-17 01:35:53.980: W/System.err(18700): at android.view.ViewGroup.<init>(ViewGroup.java:421)
09-17 01:35:53.980: W/System.err(18700): at android.widget.RelativeLayout.<init>(RelativeLayout.java:180)
09-17 01:35:53.980: W/System.err(18700): at com.example.behealthy.HttpClass.getResultData(HttpClass.java:77)
09-17 01:35:53.980: W/System.err(18700): at com.example.behealthy.GetMethod.onPostExecute(GetMethod.java:44)
09-17 01:35:53.980: W/System.err(18700): at com.example.behealthy.GetMethod.onPostExecute(GetMethod.java:1)
09-17 01:35:53.980: W/System.err(18700): at android.os.AsyncTask.finish(AsyncTask.java:631)
09-17 01:35:53.980: W/System.err(18700): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-17 01:35:53.980: W/System.err(18700): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-17 01:35:53.980: W/System.err(18700): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 01:35:53.980: W/System.err(18700): at android.os.Looper.loop(Looper.java:137)
09-17 01:35:53.980: W/System.err(18700): at android.app.ActivityThread.main(ActivityThread.java:4898)
09-17 01:35:53.985: W/System.err(18700): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 01:35:53.985: W/System.err(18700): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 01:35:53.985: W/System.err(18700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-17 01:35:53.985: W/System.err(18700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-17 01:35:53.985: W/System.err(18700): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
实例化这样的Activity
是错误的,这就是为什么你得到NPE
HttpClass hc = new HttpClass();
try {
hc.getResultData(result);
这显然是一个单独的类,所以你要做的是创建一个interface
来在任务完成时给出一个回调。
This answer举例说明interface
使用AsyncTask
。
如果它是你Activity
的内部类,那么你可以直接从onPostExecute()
调用该函数,但是如果你需要它是一个单独的文件,那么你将需要按照链接。