我正在使用下面的代码,其中ListView的onClick在浏览器中打开一个链接。有没有办法在一些吸引人的小前景活动中打开相同的浏览器,如自定义对话框或弹出窗口?有人试过吗?
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(listItems.get(pos).getLink()));
activity.startActivity(i);
}
谢谢, 预先
根据Tarun建议编辑代码:
public class ListListener extends Activity implements OnItemClickListener {
// List item's reference
List<RssItem> listItems;
// Calling activity reference
Activity activity;
public ListListener(List<RssItem> aListItems, Activity anActivity) {
listItems = aListItems;
activity = anActivity;
}
/**
* Start a browser with url from the rss item.
*/
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
/*Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(listItems.get(pos).getLink()));
activity.startActivity(i);*/
AlertDialog.Builder alert = new AlertDialog.Builder(ListListener.this);
alert.setTitle("Title here");
WebView wv = new WebView(this);
wv.loadUrl(listItems.get(pos).getLink());
wv.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
}
});
alert.show();
}
}
错误:
07-22 13:18:43.465:E / AndroidRuntime(22759):致命异常:主要 07-22 13:18:43.465:E / AndroidRuntime(22759):java.lang.NullPointerException 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:139) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.app.AlertDialog $ Builder。(AlertDialog.java:359) 07-22 13:18:43.465:E / AndroidRuntime(22759):at com.itcuties.multicategoryrssreader.listeners.ListListener.onItemClick(ListListener.java:43) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.widget.AdapterView.performItemClick(AdapterView.java:298) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.widget.AbsListView.performItemClick(AbsListView.java:1283) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3074) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.widget.AbsListView $ 1.run(AbsListView.java:4147) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.os.Handler.handleCallback(Handler.java:615) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.os.Handler.dispatchMessage(Handler.java:92) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.os.Looper.loop(Looper.java:137) 07-22 13:18:43.465:E / AndroidRuntime(22759):在android.app.ActivityThread.main(ActivityThread.java:4898) 07-22 13:18:43.465:E / AndroidRuntime(22759):at java.lang.reflect.Method.invokeNative(Native Method) 07-22 13:18:43.465:E / AndroidRuntime(22759):at java.lang.reflect.Method.invoke(Method.java:511) 07-22 13:18:43.465:E / AndroidRuntime(22759):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1008) 07-22 13:18:43.465:E / AndroidRuntime(22759):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 07-22 13:18:43.465:E / AndroidRuntime(22759):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title here");
WebView wv = new WebView(this);
wv.loadUrl("http:\\www.google.com");
wv.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
}
});
alert.show();