如何解决为什么我在android中出现异常
| 我已经编写了一个Android应用程序,该应用程序将称为REST Web服务。 首先,我尝试了固定变量的Android应用程序。我可以成功调用Web服务。 但是,当我在Android应用程序中集成GUI并在变量中分配值时,在运行时会出现以下异常:java.lang.reflect.InvocationTargetException
java.lang.NullPointerException
为什么我会得到例外?
我的程序代码是:
public class testing extends Activity {
private EditText txturl,foldername,Metadata,Username,Password;
private TextView textview;
private Button btnok,btncancel;
private String url,foldname,Metadata;
private String username,password;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnok = (Button)findViewById(R.id.btncrtfold);
btncancel=(Button)findViewById(R.id.btnreset);
btnok.setonClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
txturl=(EditText)findViewById(R.id.txturl);
Username=(EditText)findViewById(R.id.txtloginname);
Password=(EditText)findViewById(R.id.txtpassword);
foldername = (EditText)findViewById(R.id.txtfoldername);
Metadata=(EditText)findViewById(R.id.txtMetadata);
foldname=foldername.getText().toString();
Metadata=Metadata.getText().toString();
username=Username.getText().toString();
url=txturl.getText().toString();
password=Password.getText().toString();
//HttpRequestInterceptor preemptiveAuth=login();
String result=doprocess(url,username,password,Metadata,foldname);
textview.setText(result);
}});
}
public String doprocess(String url,String username,String password,String Metadata,String fold){
HttpResponse response=null;
String content=null;
//public static void login() {
HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request,HttpContext context) throws HttpException,IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(),targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
};
DefaultHttpClient httpclient = new DefaultHttpClient();
url= url+\"/\"+username+\"/\"+foldername;
HttpPut put=new HttpPut(url);
try{
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null,-1),new UsernamePasswordCredentials(username,password));
httpclient.addRequestInterceptor(preemptiveAuth,0);
httpentity data=new StringEntity(Metadata);
put.setEntity(data);
put.setHeader(\"Content-Type\",\"application/vnd.org.snia.cdmi.container\");
response = httpclient.execute(put);
httpentity resEntity=response.getEntity();
if(resEntity!=null)
content = (EntityUtils.toString(resEntity));
}catch (Exception e) {
Log.e(\"PUT Exception\",\"java.lang.NullPointerException\");
}
String result=response.getStatusLine().toString()+\"\\n\"+content;
return result;
}
解决方法
嗨,我认为您的doProcess方法不会返回任何内容,因此它将抛出
TextView方法上的setText上的NullPointerException。
请确认。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。