2012-05-18 79 views
0

因此我試圖在成功登錄後從主活動啓動WebView活動。主要的活動是這樣的:重定向到Android中的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的地方我得到debigging成功的響應,但它應該帶我去的網頁,但其他的應用程序崩潰和關閉:

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 
+0

註冊似乎是空 – keyser

+0

Registration.java看行... 19 –

+0

刪除我的回答,@SamirMangroliya在我完成打字之前在評論中提到了它。 – jjm

回答

1

的網頁視圖活動應該是這樣的:

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

    WebView mWebView = new WebView(this); 

    //now do whatever u 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); 
} 
+0

非常感謝謝里夫這解決了我的問題.. – Jawad

1

您是否在清單文件中添加了您的活動?就像這樣:

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

UPDATE

我認爲你需要交換的兩行的順序。

來源:

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); 
+0

是的,我做了,它仍然崩潰.. – Jawad

+0

@Jawad請參閱我的更新答案。 – amp

+0

我已經試過這個,logcat給我錯誤在第20行,其中包含 mWebView.loadUrl(「http://192.168.1.101/adddrop/index2.php」); 請注意,當我單獨啓動它作爲單獨項目中的主要活動時,此registration.java類完全正常工作... – Jawad

相關問題