2017-11-30 11 views
-1

インターネットが利用できない場合、インターネット接続の可用性をチェックし、警告ダイアログボックスを表示する新しい機能を追加するまで、My Appは正常に動作します。私はエミュレータでインターネットをチェックせずに私のコードを実行するとき、それはうまく動作しますが、私は私が下に示したと同じコードを使用して確認するために使用するとforce closeダイアログボックスOKボタンで私はインターネット上で利用可能なすべてのソリューションを試した私。私はAndroid開発の初心者ですが、物事は分かりませんが、私の研究によれば、私のコードでは何かが間違っていますが、残念ながら私はそれを理解することができません。ここで ダイアログボックスを追加すると、Android App強制エミュレータで停止する

はここ

package com.testapp.mytestapp; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.FrameLayout; 
import java.io.File; 
import static com.testapp.mytestapp.R.*; 

public class MyActivity extends Activity { 
    private WebView webView; 
    private FrameLayout customViewContainer; 
    private WebChromeClient.CustomViewCallback customViewCallback; 
    private View mCustomView; 
    private myWebChromeClient mWebChromeClient; 
    private myWebViewClient mWebViewClient; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(this.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = conMgr.getActiveNetworkInfo(); 

     if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) { 

      AlertDialog alertDialog; 
      alertDialog = new AlertDialog.Builder(this, style.AlertDialogCustom).create(); 
      alertDialog.setTitle("Communication Error"); 
      alertDialog.setMessage("Please make sure that internet connection is active."); 
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
       } 
      }); 
      alertDialog.show(); 
     } else { 
      setContentView(R.layout.main); 
      AppRater appRater = new AppRater(this); 
      appRater.setDaysBeforePrompt(1); 
      appRater.setLaunchesBeforePrompt(3); 
      appRater.setPhrases("Rate This App", 
        "If you like our App don't forget to rate our application on Google Play. Thanks for your support!", 
        "Rate Now", "Later", "Ignore"); 
      appRater.setTargetUri("https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName()); 
      appRater.show(); 
      customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer); 
      webView = (WebView) findViewById(R.id.webView); 
      mWebViewClient = new myWebViewClient(); 
      webView.setWebViewClient(mWebViewClient); 
      mWebChromeClient = new myWebChromeClient(); 
      webView.setWebChromeClient(mWebChromeClient); 
      webView.getSettings().setJavaScriptEnabled(true); 
      webView.getSettings().setAppCacheEnabled(true); 
      webView.getSettings().setSaveFormData(true); 
      webView.loadUrl("https://custom-domain-name.com/test-page.html"); 
     } 
    } 

    public boolean inCustomView() { 
     return (mCustomView != null); 
    } 

    public void hideCustomView() { 
     mWebChromeClient.onHideCustomView(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); //To change body of overridden methods use File | Settings | File Templates. 
     webView.onPause(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); //To change body of overridden methods use File | Settings | File Templates. 
     webView.onResume(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); //To change body of overridden methods use File | Settings | File Templates. 
     if (inCustomView()) { 
      hideCustomView(); 
     } 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 

      if (inCustomView()) { 
       hideCustomView(); 
       return true; 
      } 

      if ((mCustomView == null) && webView.canGoBack()) { 
       webView.goBack(); 
       return true; 
      } 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    class myWebChromeClient extends WebChromeClient { 
     private Bitmap mDefaultVideoPoster; 
     private View mVideoProgressView; 

     @Override 
     public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) { 
      onShowCustomView(view, callback); //To change body of overridden methods use File | Settings | File Templates. 
     } 

     @Override 
     public void onShowCustomView(View view,CustomViewCallback callback) { 

      // if a view already exists then immediately terminate the new one 
      if (mCustomView != null) { 
       callback.onCustomViewHidden(); 
       return; 
      } 
      mCustomView = view; 
      webView.setVisibility(View.GONE); 
      customViewContainer.setVisibility(View.VISIBLE); 
      customViewContainer.addView(view); 
      customViewCallback = callback; 
     } 

     @Override 
     public View getVideoLoadingProgressView() { 

      if (mVideoProgressView == null) { 
       LayoutInflater inflater = LayoutInflater.from(MyActivity.this); 
       mVideoProgressView = inflater.inflate(R.layout.video_progress, null); 
      } 
      return mVideoProgressView; 
     } 

     @Override 
     public void onHideCustomView() { 
      super.onHideCustomView(); //To change body of overridden methods use File | Settings | File Templates. 
      if (mCustomView == null) 
       return; 

      webView.setVisibility(View.VISIBLE); 
      customViewContainer.setVisibility(View.GONE); 

      // Hide the custom view. 
      mCustomView.setVisibility(View.GONE); 

      // Remove the custom view from its container. 
      customViewContainer.removeView(mCustomView); 
      customViewCallback.onCustomViewHidden(); 

      mCustomView = null; 
     } 
    } 

    class myWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if(Uri.parse(url).getHost().endsWith("custom-domain-name.com")) { 
       return false; 
      } 

      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
      view.getContext().startActivity(intent); 
      return true; 
      //return super.shouldOverrideUrlLoading(view, url); //To change body of overridden methods use File | Settings | File Templates. 
     } 
    } 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     try { 
      trimCache(); //if trimCache is static 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void trimCache() { 
     try { 
      File dir = getCacheDir(); 
      if (dir != null && dir.isDirectory()) { 
       deleteDir(dir); 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 
    } 

    public static boolean deleteDir(File dir) { 
     if (dir != null && dir.isDirectory()) { 
      String[] children = dir.list(); 
      for (int i = 0; i < children.length; i++) { 
       boolean success = deleteDir(new File(dir, children[i])); 
       if (!success) { 
        return false; 
       } 
      } 
      return dir.delete(); 
     } 
     return false; 
    } 

} 

は私のAndroidManifest.xmlここ

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.testapp.mytestapp" > 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.CLEAR_APP_CACHE"/> 
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher" 
      android:hardwareAccelerated="true"> 
     <activity android:name=".MyActivity" 
        android:configChanges="orientation|keyboardHidden" 
        android:hardwareAccelerated="true" 
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

私のレイアウトのmain.xmlがある私の主な活動である

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
     > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <WebView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/webView" 
      android:layout_gravity="center" 
      /> 
    <FrameLayout 
      android:id="@+id/customViewContainer" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:visibility="gone" 
      /> 
</LinearLayout> 
ここ

は、ここに私のレイアウトのvideo_progress.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/progress_indicator" 
       android:orientation="vertical" 
       android:layout_centerInParent="true" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

    <ProgressBar android:id="@android:id/progress" 
       style="?android:attr/progressBarStyleLarge" 
       android:layout_gravity="center" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 

    <TextView android:paddingTop="5dip" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:text="loading" 
       android:textSize="14sp" 
       android:textColor="?android:attr/textColorPrimary"/> 
</LinearLayout> 

であるあなたがあなたの状態の前に、あなたの意見の参照を取得する必要があるため、クラッシュがある私ののstyles.xml

<resources> 
    <!-- Base application theme. --> 
    <color name="basic_blue1">#ffffff</color> 
    <color name="basic_blue2">#ffffff</color> 
    <color name="basic_blue3">#9954BB</color> 
    <color name="basic_orange1">#2780E3</color> 
    <color name="basic_orange2">#2780E3</color> 
    <color name="basic_white1">#eeffffff</color> 
    <color name="basic_white2">#99ffffff</color> 

    <style name="AlertDialogCustom" parent="@android:style/Theme.Holo"> 
     <item name="android:layout_gravity">center</item> 
     <item name="android:textSize">22sp</item> 
    </style> 

    <style name="app_rater" parent="@android:style/Theme.Holo"> 
     <item name="android:layout_gravity">center</item> 
     <item name="android:textSize">22sp</item> 
    </style> 

</resources> 
+0

あなたはエミュレータのバージョンは何がありますか? – diegoveloper

+0

@diegoveloper Nexus 4 API 22 – Rtra

+0

スタックトレースを見て、クラッシュの原因を特定してください:http://stackoverflow.com/questions/23353173 –

答えて

2

OKですが、 onResume/onPause/onStopメソッドでwebviewオブジェクトを使用しているため、インターネットを持たない場合、webviewオブジェクトはnullです。

移動した場合/他の前に、この行:

 setContentView(R.layout.main); 
     customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer); 
     webView = (WebView) findViewById(R.id.webView); 
+0

ああ、あなたは私の日を救ってくれました。私はあなたの優しさを本当に訴えています。大きな問題から私を救ったことを感謝する言葉はありません。 :) – Rtra

+0

問題ない男、私はあなたを助けてうれしいです。 LogCatがあなたの友人であることを忘れないでください:) – diegoveloper

+0

私はいつもそれを使用し、私が言ったように私はアンドロイドの世界に新しいです:) – Rtra

関連する問題