尝试添加下拉刷新时Webview崩溃

时间:2017-03-08 08:18:27

标签: android webview

您好我最近开始在rails网站上为我的ruby构建一个Android应用程序。我对这两者都很新,所以一团糟。 Atm我被卡住了,因为我的应用程序在尝试添加下拉刷新并开始意图后停止显示我的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>

提前致谢,我很少在任何地方发帖。但有点卡住,终于摆脱了所有的错误,所以我不知道还有什么去谷歌:)

2 个答案:

答案 0 :(得分:1)

WebView包裹在PullToRefreshWebView内。创建布局文件结构:

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

答案 1 :(得分:0)

弄清楚如何正确刷新:

https://github.com/chrisbanes/Android-PullToRefresh/blob/master/sample/src/com/handmark/pulltorefresh/samples/PullToRefreshWebViewActivity.java

添加:

PullToRefreshWebView mPullRefreshWebView;
WebView mWebView;

MainActivity

并且,这在onCreate方法下:

mPullRefreshWebView = (PullToRefreshWebView) findViewById(R.id.pull_refresh_webview);
mWebView = mPullRefreshWebView.getRefreshableView();