我已經通過大量帖子和論壇,但找不到解決方案。我正在開發與本地HTML文件的應用程序中的我已經這一項主要活動變量視圖可能沒有初始化並且從內部視圖訪問變量視圖
package com.faraksoch.sagar.eroutine;
public class MainActivity extends AppCompatActivity {
boolean doubleBackToExitPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
findViewById(R.id.image1).setVisibility((View.GONE));
findViewById(webView).setVisibility(View.VISIBLE);
}
});
webView.loadUrl("file:///android_asset/index.html");
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
Intent intent = new Intent(MainActivity.this, UpdateService.class);
startService(intent);
}
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit",
Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
WebView webView;
public void goBack(){
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
}符合
這裏
WebView webView = (WebView) findViewById(webView);
我得到web視圖的錯誤可能尚未初始化並在線
findViewById(webView).setVisibility(View.VISIBLE);
變量webView是從innter類訪問whi ch需要聲明爲final,因此當我將代碼更改爲此時,
final WebView webView = (WebView) findViewById(webView);
仍然沒有運氣。
和我Main_activity文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.faraksoch.sagar.eroutine.MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="368dp"
android:layout_height="576dp"
android:background="@drawable/ssplash" />
</android.support.constraint.ConstraintLayout>
誰能幫我這個問題,我必須做什麼,使其工作。
我該如何做到最後。我應該在哪裏放置什麼代碼才能最終完成? –