当我点击"登录"我需要你帮助应用程序崩溃按钮,我做错了什么? (该应用程序应该在webview中打开一个新活动) 谢谢你的回答, 我有2个活动, 主要代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
LoadPage();
}
public Button Btn;
public void LoadPage(){
Btn = (Button)findViewById(R.id.LoginBtn);
Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, WebPage.class);
startActivity(intent);
final EditText username = (EditText) findViewById(R.id.LoginTx);
intent.putExtra("Username", username.getText().toString());
final EditText password = (EditText) findViewById(R.id.PasswordTx);
intent.putExtra("Password", password.getText().toString());
}
});
}
WebPage活动代码:
public class WebPage extends AppCompatActivity {
static WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String user = getIntent().getExtras().getString("Username");
final String pwd = getIntent().getExtras().getString("Password");
setContentView(R.layout.activity_web_page);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
mWebView = (WebView) findViewById(R.id.webView2);
String url = "http://www.webnotes.cz/";
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:document.getElementById('UID0').value = '" +user+ "';document.getElementById('PASS0').value='" +pwd+ "';javascript:document.getElementById('butLogon').click();");
}
});
}
} 在调试中:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.erikstoklasa.test, PID: 13544
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erikstoklasa.test/com.example.erikstoklasa.test.WebPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2626)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2695)
at android.app.ActivityThread.access$800(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5787)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1009)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:804)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.example.erikstoklasa.test.WebPage.onCreate(WebPage.java:16)
at android.app.Activity.performCreate(Activity.java:6105)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2579)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2695)
at android.app.ActivityThread.access$800(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5787)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1009)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:804)