我正试图在Android浏览器中启动打开链接的意图。
我已经使用XML pull解析器填充了listview,其中包含每个项目的url。
奇怪的是,前几个项目工作正常,它需要我下载链接,但下面的项目不会工作,它会在点击时崩溃。
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String url = worldpopulationlist.get(position).getPopulation();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
context.startActivity(i);
}
});
Logcat错误:
FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
at android.app.Activity.startActivity(Activity.java:3627)
at android.app.Activity.startActivity(Activity.java:3595)
at Search.ListViewAdapter$1.onClick(ListViewAdapter.java:95)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
编辑:
这是我的SerachActivity.java
public class SearchActivity extends Fragment {
// Declare Variables
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
static String RATINGPOINTS = "ratingPoints";
static String APPHEADLINE = "appHeadline";
static String STOREURL = "storeURL";
static String ICONURL = "iconUrl";
EditText editsearch;
private List<mobAppModel> worldpopulationlist = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("mobAppModel", "OnCreate()");
View rootView = inflater.inflate(R.layout.listview_main, container, false);
// Execute DownloadXML AsyncTask
new DownloadXML().execute();
return rootView; }
// DownloadXML AsyncTask
private class DownloadXML extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
// mProgressDialog.setTitle("Please wait");
// // Set progressdialog message
// mProgressDialog.setMessage("Loading...");
// mProgressDialog.setIndeterminate(false);
// // Show progressdialog
// mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
worldpopulationlist = new ArrayList<mobAppModel>();
XMLParser parser = new XMLParser();
// Retrive nodes from the given website URL in XMLParser.class
String xml = parser.getXmlFromUrl("https://dl.dropboxusercontent.com/s/te0c0s7y7zr79tm/kategorijeXML.xml");
// Retrive DOM element
Document doc = parser.getDomElement(xml);
try {
// Locate the NodeList name
NodeList nl = doc.getElementsByTagName("appInfo");
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
mobAppModel map = new mobAppModel();
map.setRank(parser.getValue(e, RATINGPOINTS));
map.setCountry(parser.getValue(e, APPHEADLINE));
map.setPopulation(parser.getValue(e, STOREURL));
map.setFlag(parser.getValue(e, ICONURL));
worldpopulationlist.add(map);
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) getView().findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(getActivity(),
worldpopulationlist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Locate the EditText in listview_main.xml
editsearch = (EditText) getView().findViewById(R.id.search);
// Capture Text in EditText
editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = editsearch.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
}
}
}
这是我的ListViewAdapter,其中包含intent:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
private List<mobAppModel> worldpopulationlist = null;
private ArrayList<mobAppModel> arraylist;
public ListViewAdapter(Context context,
List<mobAppModel> worldpopulationlist) {
this.context = context;
this.worldpopulationlist = worldpopulationlist;
inflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<mobAppModel>();
this.arraylist.addAll(worldpopulationlist);
imageLoader = new ImageLoader(context);
}
public class ViewHolder {
TextView ocjena;
TextView naziv;
TextView developer;
ImageView iconUrl;
}
@Override
public int getCount() {
return worldpopulationlist.size();
}
@Override
public Object getItem(int position) {
return worldpopulationlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.ocjena = (TextView) view.findViewById(R.id.ocjena);
holder.naziv = (TextView) view.findViewById(R.id.naziv);
holder.developer = (TextView) view.findViewById(R.id.developer);
// Locate the ImageView in listview_item.xml
holder.iconUrl = (ImageView) view.findViewById(R.id.iconUrl);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.ocjena.setText(worldpopulationlist.get(position).getRank());
holder.naziv.setText(worldpopulationlist.get(position).getCountry());
holder.developer.setText(worldpopulationlist.get(position)
.getPopulation());
// Set the results into ImageView
imageLoader.DisplayImage(worldpopulationlist.get(position).getFlag(),
holder.iconUrl);
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String url = worldpopulationlist.get(position).getPopulation();
Intent i = new Intent(context, ListViewAdapter.class);
i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
context.startActivity(i);
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
worldpopulationlist.clear();
if (charText.length() == 0) {
worldpopulationlist.addAll(arraylist);
} else {
for (mobAppModel wp : arraylist) {
if (wp.getCountry().toLowerCase(Locale.getDefault())
.contains(charText)) {
worldpopulationlist.add(wp);
}
}
}
notifyDataSetChanged();
}
}
SS:
答案 0 :(得分:1)
您不应为列表中的每个项目设置onClickListener
。这是非常低效的。相反,您应在活动中的onItemClickListener
上设置ListView
。单击某个项目时,将使用单击列表中的位置调用此方法。