为什么我在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;
}
I have written an Android application which will call a REST Web Service.
Firstly, I tried an Android application with variable is fixed. I can successfully call the Web Service .
But when I integrated a GUI in the Android application and assigned value in variable, at runtime I get the following exception:
java.lang.reflect.InvocationTargetException
java.lang.NullPointerException
Why do I get the exception?
My program code is :
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您好,我认为您的 doProcess 方法不会返回任何内容,因此它会
在 Textview 方法的 setText 上抛出 NullPointerException 。
请确认。
Hi I think your doProcess method doesn't return anything so that it will throw
NullPointerException on setText on Textview's method.
Please confirm.