Java Android Studio-在活动之间切换-findViewById为空
我在一个基本活动中有一个抽屉布局。 我的所有活动都继承自此基本活动 我有两项活动。 主要活动 活动2Java Android Studio-在活动之间切换-findViewById为空,java,android,Java,Android,我在一个基本活动中有一个抽屉布局。 我的所有活动都继承自此基本活动 我有两项活动。 主要活动 活动2 WebView myWebView = (WebView) findViewById(R.id.webview); WebView myWebView = (WebView) findViewById(R.id.webview); if(myWebView == null) { Intent intent = new Intent(getApplicatio
WebView myWebView = (WebView) findViewById(R.id.webview);
WebView myWebView = (WebView) findViewById(R.id.webview);
if(myWebView == null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
myWebView = (WebView) findViewById(R.id.webview);
//myWebView IS STILL NULL!!
//Some click event in Action2
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Intent intent = new Intent();
//Pass information back to MainActivity
intent.putExtra("my_url", url);
//Tell MainActivity everything worked OK in Activity2
setResult(RESULT_OK, intent);
//End Activity2
finish();
}
只要我在MainActivity,我的WebView也在那里,一切都很好
但是当我参加活动的时候
WebView myWebView = (WebView) findViewById(R.id.webview);
WebView myWebView = (WebView) findViewById(R.id.webview);
if(myWebView == null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
myWebView = (WebView) findViewById(R.id.webview);
//myWebView IS STILL NULL!!
//Some click event in Action2
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Intent intent = new Intent();
//Pass information back to MainActivity
intent.putExtra("my_url", url);
//Tell MainActivity everything worked OK in Activity2
setResult(RESULT_OK, intent);
//End Activity2
finish();
}
myWebView为空
那么,我如何才能回到主要活动
然后当我在Activity2上时,设置和URL到myWebView
当我尝试这一点,我在活动2
WebView myWebView = (WebView) findViewById(R.id.webview);
WebView myWebView = (WebView) findViewById(R.id.webview);
if(myWebView == null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
myWebView = (WebView) findViewById(R.id.webview);
//myWebView IS STILL NULL!!
//Some click event in Action2
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Intent intent = new Intent();
//Pass information back to MainActivity
intent.putExtra("my_url", url);
//Tell MainActivity everything worked OK in Activity2
setResult(RESULT_OK, intent);
//End Activity2
finish();
}
所以我希望你明白我的问题是什么
WebView位于MainActivity上。
如果我在Activity2上,我想返回MainActivity,然后设置WebView的url
我尝试了很多东西,比如onBackPressed();或完成()
但对我来说什么都不管用
非常感谢当您在Activity2中时,无法获取对MainActivity中声明的Webview的引用。如果Activity2需要自己的Webview,它必须在自己的布局xml文件中声明Webview,您可以在设置布局充气器时指向该文件。如果Activity2需要将一些信息(如URL)传递回MainActivity,则在返回MainActivity之前,它会以一种意图这样做。这需要MainActivity使用startActivityForResult() 在MainActivity.java中
Intent iIntent = new Intent(thisContext, TripActivity.class);
TripItem trip = new TripItem(0, 0, employeeID);
iIntent.putExtra("trip", trip);
startActivityForResult(iIntent, ACTIVITY_TRIP);
.....
public void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
super.onActivityResult(requestCode, resultCode, returnedIntent);
switch(requestCode) {
case ACTIVITY_TRIP:
if (resultCode == android.app.Activity.RESULT_OK){
String url = returnedIntent.getStringExtra("my_url", false);
myWebview.loadUrl(url);
}
break;
...
在活动中2
WebView myWebView = (WebView) findViewById(R.id.webview);
WebView myWebView = (WebView) findViewById(R.id.webview);
if(myWebView == null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
myWebView = (WebView) findViewById(R.id.webview);
//myWebView IS STILL NULL!!
//Some click event in Action2
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Intent intent = new Intent();
//Pass information back to MainActivity
intent.putExtra("my_url", url);
//Tell MainActivity everything worked OK in Activity2
setResult(RESULT_OK, intent);
//End Activity2
finish();
}
是否要将第一个活动的url传递给第二个活动?您的webview为空,因为您没有为webview设置任何值。因此,只需使用intent传递该值,并将其设置为webviewshare,即可共享这两个活动的布局xml。