我想在一個webview中打開一個URL,這個URL與我在JSONArray中獲取的URL不同。在Webview中從json數組打開URL
MainActivity類別(我能利用我在我的類創建的getURL方法,其獲取的JSON並解析到一個數組獲取舉杯消息的網址和顯示)
@Override
protected void onPostExecute(Integer result) {
final Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
progressBar.setVisibility(View.GONE);
if (result == 1) {
adapter = new MyRecyclerViewAdapter(MainActivity.this, feedsList);
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(FeedItem item) {
Toast.makeText(MainActivity.this, item.getUrl(), Toast.LENGTH_LONG).show();
// Here is what I've tried below but doesn't work
String blogUrl = item.getUrl();
Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
intent.setData(Uri.parse(blogUrl));
startActivity(intent);
}
});
}
}
這是我的WebViewActivity類
public class WebViewActivity extends AppCompatActivity {
public WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
String url = getIntent().getStringExtra("url");
myWebView = (WebView) findViewById(R.id.webView);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
}
當點擊我的RecyclerView的項目它打開了網頁流量的活動,但只是顯示一個空白的屏幕,如果我評論說出來,只有它顯示了正確的吐司消息會發生什麼URL。我究竟做錯了什麼?
- 編輯 - 我在AndroidManifest定義以及
<activity
android:name=".WebViewActivity"
android:label="@string/title_activity_web_view"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mts2792.fishnews.MainActivity" />
</activity>
您是否已將互聯網權限添加到清單中? – Jordan
是的,我添加了<使用權限的android:name =「android.permission.INTERNET」/> –