根据 Web 服务的响应转移到新活动
我正在实现一个基于 Web 服务的特定响应的代码,该代码调用我的项目中的下一个活动。我遇到了一些问题,因为它导致了 NullPointerException
这是我的 Android 代码:
package com.example;
import java.net.SocketException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class TeacherLogin2Activity extends Activity
{
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://10.0.2.2/loginteacher/Service1.asmx";
String t_id;
String password;
String ahead;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button signin = (Button) findViewById(R.id.regsubmitbtn);
signin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText etxt_user = (EditText) findViewById(R.id.usereditlog);
t_id = etxt_user.getText().toString();
EditText etxt_password = (EditText) findViewById(R.id.pwdeditlog);
password = etxt_password.getText().toString();
new LoginTask().execute();
}
});
}
private String doLogin(String t_id, String password) {
String res=null;
final String SOAP_ACTION = "http://tempuri.org/GetLogin";
final String METHOD_NAME = "GetLogin";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("teacherid", t_id);
request.addProperty("password",password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; // Set this variable to true for
// compatibility with what seems to be the
// default encoding for .Net-Services.
envelope.setOutputSoapObject(request);
System.out.println(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());
System.out.println("response" +response);
res=response.toString();
System.out.println(res);
}catch(SocketException ex)
{
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
}
catch (Exception e) {
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
return res;
}
private class LoginTask extends AsyncTask<Void, String, String> {
private final ProgressDialog dialog = new ProgressDialog(
TeacherLogin2Activity.this);
protected void onPreExecute() {
this.dialog.setMessage("Logging in...");
this.dialog.show();
}
protected String doInBackground(final Void... unused) {
String auth=doLogin(t_id,password);
System.out.println(auth);
return null; // don't interact with the ui!
}
protected void onPostExecute(String result)
{
ahead=result;
if(result.equals("Welcome to this activity")){
Intent showContent = new Intent(getApplicationContext(),newscreen.class);
startActivity(showContent);
}
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
}
更新的 Logcat 也显示运行时异常:
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): FATAL EXCEPTION: main
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.newscreen}: java.lang.NullPointerException
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.os.Looper.loop(Looper.java:123)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at java.lang.reflect.Method.invoke(Method.java:521)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at dalvik.system.NativeStart.main(Native Method)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): Caused by: java.lang.NullPointerException
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at com.example.newscreen.onCreate(newscreen.java:25)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-28 19:40:00.099: ERROR/AndroidRuntime(1001): ... 11 more
我已将所有必需的 Internet 权限放入我的 AndroidManifest.xml 文件中..有人可以帮助解决吗我的问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
result 为 Null,因此它抛出
Null Pointer Exception
,尝试打印结果并使用 Log.txt 检查即将发生的情况。将结果存储在全局变量中并在 Asyn 任务内部访问它。
例如:在 Activity 之前将变量声明为
String Final_result
并将结果存储为final_result=result
然后比较
final_result.equalsIgnoreCase("BLA BLA BLA");
result is coming as Null so it is throwing
Null Pointer Exception
, try to print result and check what is coming using Log.Store result in Global variable and access it inside of Asyn task.
Eg:Declare Variable as
String final_result before of Activity
and store result asfinal_result=result
then Compare
final_result.equalsIgnoreCase("BLA BLA BLA");
问题是 doInBackground 的返回值,即 null。
在 onPostExecute 中,您调用 null 的 equals 方法...
按以下方式编辑代码:
The problem is the return value from doInBackground, that is null.
In onPostExecute, you call the equals method of null...
Edit you code in this way: