0

所以我试图在成功登录后从主要活动启动 WebView 活动。主要活动如下:

              try {

                response = CustomHttpClient.executeHttpPost("http://192.168.1.101/adddrop/login.php", postParameters);

                Log.v(LOG_TAG," PostParameters = " +postParameters.toString());

                 String res=response.toString();
                 res= res.replaceAll("\\s+","");

                 Log.v(LOG_TAG," response value = " +res);


                if(res.equals("1")){

                    Intent myIntent = new Intent(v.getContext(), Registration.class);
                    startActivity(myIntent);

                    //error.setText("Correct Username or Password");
                }
                else
                    error.setText("Sorry!! Incorrect Username or Password");

            } catch (Exception e) {
                USER.setText(e.toString());
            }

另一个活动WebView(编辑:称为Registration.java,当我将它作为单独项目中的主要活动启动时工作正常)看起来像这样:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.loadUrl("http://192.168.1.101/adddrop/index2.php");
    mWebView.setInitialScale(130);


}

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

这是单击登录后的logcat,我在其中得到成功的调试响应,但它应该带我进入网页,但应用程序崩溃并关闭:

05-19 12:02:39.394: W/dalvikvm(433): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-18 11:24:22.524: E/AndroidRuntime(1094): FATAL EXCEPTION: main
05-18 11:24:22.524: E/AndroidRuntime(1094): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.Registration}: java.lang.NullPointerException
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.os.Looper.loop(Looper.java:123)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at java.lang.reflect.Method.invoke(Method.java:507)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at dalvik.system.NativeStart.main(Native Method)
05-18 11:24:22.524: E/AndroidRuntime(1094): Caused by: java.lang.NullPointerException
05-18 11:24:22.524: E/AndroidRuntime(1094):     at com.example.login.Registration.onCreate(Registration.java:19)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-18 11:24:22.524: E/AndroidRuntime(1094):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-18 11:24:22.524: E/AndroidRuntime(1094):     ... 11 more
4

2 回答 2

1

您是否在清单文件中添加了活动?像这样:

<activity 
        android:name="your.package.name.Registration"
        android:label="@string/app_name" >
    </activity>

更新

我认为您需要交换两行的顺序。

从:

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl("http://192.168.1.101/adddrop/index2.php");
mWebView.setInitialScale(130);

至:

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://192.168.1.101/adddrop/index2.php");
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setInitialScale(130);

并且不要忘记在您的清单上添加 Internet 权限...

<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>

更新 2

改变这个:

Intent myIntent = new Intent(v.getContext(), Registration.class);

至:

Intent myIntent = new Intent(YourActivity.this, Registration.class);
于 2012-05-18T12:24:15.413 回答
1

您的 webview 活动应该是这样的:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView mWebView = new WebView(this);

    //now do whatever you want
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.loadUrl("http://192.168.1.101/adddrop/index2.php");
    mWebView.setInitialScale(130);

    //finally
    setContentView(mWebView);
}
于 2012-05-20T17:58:33.110 回答