2016-01-13 65 views
-1

因此,我決定爲我的網站製作一個Android應用程序(不是很多人閱讀它,但是我認爲它會在我的Ucas上看起來不錯! 我的問題在我的手機上顯示啓動畫面後,應用程序崩潰,我對Android應用程序開發頗爲陌生,所以我不確定自己做錯了什麼。Android:WebView應用程序在顯示啓動畫面3秒後崩潰

我已將我的源代碼上傳到Github這樣你可以在它更好看

繼承人我的一些代碼:

MainActivity.java

package com.alffiw.alffisblog; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ProgressBar; 


public class MainActivity extends Activity { 

    private WebView mWebView; 
    ProgressBar progressBar; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mWebView = (WebView) findViewById(R.id.activity_main_webview); 

     progressBar = (ProgressBar) findViewById(R.id.progressBar1); 

     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     mWebView.loadUrl("http://www.coderefer.com"); 
     mWebView.setWebViewClient(new HelloWebViewClient()); 


    } 

    private class HelloWebViewClient extends WebViewClient{ 


     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // TODO Auto-generated method stub 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView webView, String url) 
     { 
      webView.loadUrl(url); 
      return true; 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 

      progressBar.setVisibility(view.GONE); 
     } 

    } 


    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { //if back key is pressed 
     if((keyCode == KeyEvent.KEYCODE_BACK)&& mWebView.canGoBack()) 
     { 
      mWebView.goBack(); 
      return true; 

     } 

     return super.onKeyDown(keyCode, event); 

    } 




    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public void onBackPressed() { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
       MainActivity.this); 

     // set title 
     alertDialogBuilder.setTitle("Exit"); 

     // set dialog message 
     alertDialogBuilder 
       .setMessage("Do you really want to exit?") 
       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         // if this button is clicked, close 
         // current activity 
         MainActivity.this.finish(); 
        } 
       }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         // if this button is clicked, just close 
         // the dialog box and do nothing 
         dialog.cancel(); 
        } 
       }); 

     // create alert dialog 
     AlertDialog alertDialog = alertDialogBuilder.create(); 

     // show it 
     alertDialog.show(); 
    } 
} 

SplashScreen.java

package com.alffiw.alffisblog; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class SplashScreen extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 

     Thread timerThread = new Thread(){ 
      public void run(){ 
       try{ 
        sleep(3000); 
       }catch(InterruptedException e){ 
        e.printStackTrace(); 
       }finally{ 
        Intent intent = new Intent(SplashScreen.this,MainActivity.class); 
        startActivity(intent); 
       } 
      } 
     }; 
     timerThread.start(); 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     finish(); 
    } 

} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"> 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

    <ProgressBar 
     android:layout_centerHorizontal="true" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:id="@+id/progressBar1"/> 

    <TextView 
     android:layout_below="@+id/progressBar1" 
     android:layout_height="wrap_content" 
     android:id="@+id/LoadingText" 
     android:layout_width="fill_parent" 
     android:text="Loading, Please Wait.." 
     android:textSize="20sp" 
     android:gravity="center_horizontal"> 
    </TextView> 

    <WebView 
     android:id="@+id/activity_main_webview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    </RelativeLayout> 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.alffiw.alffisblog.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" /> 
</RelativeLayout> 

splash.xml

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

</LinearLayout> 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.alffiw.alffisblog"> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name=".SplashScreen" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

感謝您的幫助提前!

+2

崩潰發生時,logcat中有什麼? –

回答

0

你不能在你創建的線程中調用startActivity(intent)表單。首先,因爲startActivity(...)是一個Context方法,其次,您需要在UI線程上執行此語句。

試試這個:

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    ... 

    final Context activity = this; 
    Thread timerThread = new Thread(new Runnable(
      public void run(){ 
       try{ 
        sleep(3000); 
       }catch(InterruptedException e){ 
        e.printStackTrace(); 
       }finally{ 
        activity.runOnUiThread(new Runnable() { 
         public void run(){ 
          Intent intent = new Intent(SplashScreen.this,MainActivity.class); 
          activity.startActivity(intent); 
         } 
        });      
       } 
      } 
    )); 
    timerThread.start(); 
+0

謝謝你,這真的很好! –

1

而不是從線程使用Handler

protected void onCreate(Bundle savedInstanceState) { 

    //your stuff..... 
    new android.os.Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      if (!isFinishing()) {//just to make sure user didn,t press back button 
       Intent mainIntent = new Intent(Splashscreen.this, MainActivity.class); 
       startActivity(mainIntent); 
       finish(); 
      } 
     } 
    }, 3000);//waiting time 

} 

Happy_Coding調用的;