【发布时间】:2014-04-01 09:48:11
【问题描述】:
我真的不知道我的android代码中的错误是什么,希望有人可以帮助我
import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ProductCollection extends Activity {
private LayoutInflater inflater;
private ViewGroup container;
private static final ResourceBundle rb = ResourceBundle.getBundle("com.example.mobile_e_commerce.webserviceurl");
private final String NAMESPACE = rb.getString("WSDLTargetNamespace");
private final String URL = rb.getString("SoapAddress");
private final String SOAP_ACTION = rb.getString("SoapAction");
private final String METHOD_NAME = rb.getString("OperationName");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_collection);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
这个方法执行后会报错!
@Override
protected void onResume(){
super.onResume();
try{
(new dataLoader()).execute();
}
catch(Exception ee)
{
TextView textView = (TextView)findViewById(R.id.Product1);
textView.setText(ee.toString());
}
}
错误应该来自doInBackground方法
class dataLoader extends AsyncTask<TextView,String,TextView>{
ProductCollection pc = new ProductCollection();
@Override
protected TextView doInBackground(TextView... arg0) {
TextView textView = new TextView(pc);
textView = (TextView)findViewById(R.id.Product1);
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE("http://192.168.10.12:57491/MEC_WebService.asmx");
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
textView.setText("Hello World");
}
catch (Exception exception)
{
textView.setText(exception.toString());
}
return textView;
}
// @Override
// protected void onPostExecute(TextView response) {
// response = textView;
// super.onPostExecute(response);
//}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.product_collection, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_product_collection, container, false);
return rootView;
}
}
}
【问题讨论】:
-
向我们展示一个 logcat 堆栈跟踪,先生。
-
您正在更改 doInBackground 中的 UI,这是您无法做到的。将
textView.setText("Hello World");移至 onPostExecute() -
在我更改了我的 gt java.lang.NullPointerException 并且我已经将 TextView textView = new TextView(pc) 移到了方法之外,以便 onPostExecute 中的 textview.settext 也可以被初始化...... .
标签: java android android-asynctask