2017-03-08 48 views
2

您好,我最近開始爲我的紅寶石在rails網站上構建一個android應用程序。即時新聞兩個,所以它都是一團糟。 Atm我被卡住了,因爲我的應用程序在嘗試添加下拉刷新並開始意圖後停止顯示我的webview。Webview崩潰時試圖添加下拉刷新

這是我MainActivity.java

package biz.bdtp.bpin; 

import android.support.v7.app.AppCompatActivity; 
//import android.view.View; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.content.Intent; 
import android.net.Uri; 
import java.util.ArrayList; 
import android.os.AsyncTask; 
import android.widget.ProgressBar; 

import com.handmark.pulltorefresh.library.PullToRefreshBase; 
import com.handmark.pulltorefresh.library.PullToRefreshWebView; 


public class MainActivity extends AppCompatActivity { 
public WebView webb; 
ProgressBar progressBar; 


@Override 
public void onBackPressed() { 
    if (webb.canGoBack()) { 
     webb.goBack(); 
    } else { 
     super.onBackPressed(); 
    } 

} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 


    setContentView(R.layout.activity_main); 
    webb = (WebView) findViewById(R.id.web1); 

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

    webb.setWebViewClient(new WebViewClient()); 
    webb.getSettings().setJavaScriptEnabled(true); 
    webb.getSettings().setBuiltInZoomControls(true); 
    webb.getSettings().setDisplayZoomControls(false); 

    String url = "http://www.bdtp.biz:3000"; 
    webb.loadUrl(url); 


    // Get intent, action and MIME type 
    Intent intent = getIntent(); 
    String action = intent.getAction(); 
    String type = intent.getType(); 

    if (Intent.ACTION_SEND.equals(action) && type != null) { 
     if ("text/plain".equals(type)) { 
      handleSendText(intent); // Handle text being sent 
     } else if (type.startsWith("image/")) { 
      handleSendImage(intent); // Handle single image being sent 
     } 
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { 
     if (type.startsWith("image/")) { 
      handleSendMultipleImages(intent); // Handle multiple images being sent 
     } 
    } else { 
     // Handle other intents, such as being started from the home screen 
    } 

} 


void handleSendText(Intent intent) { 
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); 
    if (sharedText != null) { 
     // Update UI to reflect text being shared 
    } 
} 

void handleSendImage(Intent intent) { 
    Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); 
    if (imageUri != null) { 
     // Update UI to reflect image being shared 
    } 
} 

void handleSendMultipleImages(Intent intent) { 
    ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); 
    if (imageUris != null) { 
     // Update UI to reflect multiple images being shared 
    } 

    // Set a listener to be invoked when the list should be refreshed. 
    PullToRefreshWebView pullToRefreshView = (PullToRefreshWebView) findViewById(R.id.pull_to_refresh_webview); 
    pullToRefreshView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<WebView>() { 
     @Override 
     public void onRefresh(PullToRefreshBase<WebView> refreshView) { 
      // Do work to refresh the list here. 
      new GetDataTask(refreshView).execute(); 

     } 
    }); 

} 



private class GetDataTask extends AsyncTask<Void, Void, String[]> { 

    PullToRefreshBase<?> mRefreshedView; 
    private GetDataTask(PullToRefreshBase<?> refreshedView) { 
     mRefreshedView = refreshedView; 
    } 

    @Override 
    protected String[] doInBackground(Void... params) { 
     // Simulates a background job. 
     try { 
      Thread.sleep(4000); 
     } catch (InterruptedException e) { 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String[] result) { 
     // Call onRefreshComplete when the list has been refreshed. 
     mRefreshedView.onRefreshComplete(); 
     super.onPostExecute(result); 

    } 


} 

private class MyWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 
} 


} 

這是我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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="biz.bdtp.bpin.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"/> 

<WebView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/web1" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentBottom="true" /> 

<com.handmark.pulltorefresh.library.PullToRefreshWebView 

    android:id="@+id/pull_to_refresh_webview" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:cacheColorHint="#00000000" 
    android:divider="#19000000" 
    android:dividerHeight="4dp" 
    android:fadingEdge="none" 
    android:fastScrollEnabled="false" 
    android:footerDividersEnabled="false" 
    android:headerDividersEnabled="false" 
    android:scrollbars="none" 
    android:smoothScrollbar="true" 
    /> 
</RelativeLayout> 

這是事先我艙單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="biz.bdtp.bpin"> 

<uses-permission android:name="android.permission.INTERNET" /> 





<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 


     <activity android:name="MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.SEND" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="image/*" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.SEND" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="text/plain" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.SEND_MULTIPLE" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="image/*" /> 
      </intent-filter> 
     </activity> 


</application> 

</manifest> 

謝謝,我很少交任何地方。但有點卡住,終於擺脫了所有錯誤,所以我不知道還有什麼谷歌:)

+0

發表您的錯誤日誌 –

+0

請加你崩潰日誌 –

回答

1

把你的WebView換成PullToRefreshWebView。創建佈局文件結構:

<com.handmark.pulltorefresh.library.PullToRefreshWebView> 
    <WebView /> 
</com.handmark.pulltorefresh.library.PullToRefreshWebView> 
+0

棒極了感謝,現在的工作。它不會實際刷新,但它通過運動大聲笑 –

+0

很高興,它可以幫助你。我有一個'WebView'的回購,你可以在這裏看到,如果你有興趣:https://github.com/SatanPandeya/file_chooser_webview這是用於演示。儘管我已經實現了'SwipeToRefreshLayout'。 –