我有一个带有JSON列表视图的片段布局,但是当我点击它们时,我收到了错误。
06-13 13:17:58.492: E/AndroidRuntime(1542): FATAL EXCEPTION: main
06-13 13:17:58.492: E/AndroidRuntime(1542): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.yeftaandrea.plesirsolo/com.yeftaandrea.plesirsolo.KaranganyarAttractionDetailFragment}; have you declared this activity in your AndroidManifest.xml?
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.app.Activity.startActivityForResult(Activity.java:3390)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.app.Activity.startActivityForResult(Activity.java:3351)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:848)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.support.v4.app.Fragment.startActivity(Fragment.java:878)
06-13 13:17:58.492: E/AndroidRuntime(1542): at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment$1.onItemClick(KaranganyarAttractionFragment.java:80)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.widget.AbsListView$1.run(AbsListView.java:3463)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.os.Handler.handleCallback(Handler.java:730)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.os.Looper.loop(Looper.java:137)
06-13 13:17:58.492: E/AndroidRuntime(1542): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-13 13:17:58.492: E/AndroidRuntime(1542): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 13:17:58.492: E/AndroidRuntime(1542): at java.lang.reflect.Method.invoke(Method.java:525)
06-13 13:17:58.492: E/AndroidRuntime(1542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-13 13:17:58.492: E/AndroidRuntime(1542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-13 13:17:58.492: E/AndroidRuntime(1542): at dalvik.system.NativeStart.main(Native Method)
这是我的源代码:
1.KaranganyarAttractionFragment
package com.yeftaandrea.plesirsolo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import com.yeftaandrea.plesirsolo.adapter.KaranganyarJsonAttractionAdapter;
public class KaranganyarAttractionFragment extends Fragment {
// URL to get contacts JSON
// JSON Node names
public static final String TAG_ID = "ID";
public static final String TAG_NAME = "Name";
public static final String TAG_ADDRESS = "Address";
public static final String TAG_PHONE = "Phone";
public static final String TAG_CITY = "City";
public static final String TAG_DESCRIPTION = "Description";
public static final String TAG_AKSESBILITAS = "Aksesbilitas";
public static final String TAG_Tiket = "Tiket";
public static final String TAG_FACILITIES = "Faclilities";
public static final String TAG_LANTITUDE = "Lantitude";
public static final String TAG_LONGTITUDE = "Longtitude";
public static final String TAG_PICPATH = "PicPath";
// Hashmap for ListView
ArrayList<HashMap<String, String>> attractionList;
ListView list;
KaranganyarJsonAttractionAdapter adapter;
// Progress Dialog
private ProgressDialog pDialog;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_attraction_karanganyar, container, false);
list = (ListView) rootView.findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String name = ((TextView) view.findViewById(R.id.Name))
.getText().toString();
String address = ((TextView) view.findViewById(R.id.Address))
.getText().toString();
String phone = ((TextView) view.findViewById(R.id.Phone))
.getText().toString();
Intent i = new Intent(getActivity().getApplicationContext(), KaranganyarAttractionDetailFragment.class);
i.putExtra("TAG_ID", id);
i.putExtra("TAG_NAME", name);
i.putExtra("TAG_ADDRESS", address);
i.putExtra("TAG_PHONE", phone);
startActivity(i);
}
});
// Calling async task to get json
new LoadTask().execute();
return rootView;
}
public void loadAttraction(){
attractionList = new
ArrayList<HashMap<String, String>>();
JSONParser parser = new JSONParser();
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {
JSONArray attraction = parser.makeHttpRequestArray("http://plesirsolo.meximas.com/plesir/attraction/wisata/karanganyar/", "GET", params);
// JSONArray attraction = parser.makeHttpRequestArray("http://192.168.56.1:8080/examples/json.html", "GET", params);
// looping through All messages
if(attraction!=null){
for (int i = 0; i < attraction.length(); i++) {
JSONObject c = attraction.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String nama = c.getString(TAG_NAME);
String address = c.getString(TAG_ADDRESS);
String phone = c.getString(TAG_PHONE);
String picPath = c.getString(TAG_PICPATH);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, nama);
map.put(TAG_ADDRESS, address);
map.put(TAG_PHONE, phone);
map.put(TAG_PICPATH, picPath);
// adding HashList to ArrayList
attractionList.add(map);
}
adapter = new KaranganyarJsonAttractionAdapter(
getActivity(), attractionList);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private class LoadTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading Attraction ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
loadAttraction();
return null;
}
@Override
protected void onPostExecute(Void result) {
// dismiss the dialog after getting all products
pDialog.dismiss();
super.onPostExecute(result);
list.setAdapter(adapter);
Log.i("JSON", "TEST");
Log.i("JSON", attractionList.toString());
}
}
}
KaranganyarJsonAttractionAdapter
公共类KaranganyarJsonAttractionAdapter扩展了BaseAdapter {
private ArrayList<HashMap<String, String>> data;
public ImageLoader imageLoader;
private Context context;
public String imageBaseDirectory = "http://plesirsolo.meximas.com/plesir/images/wisata/";
public KaranganyarJsonAttractionAdapter(Context context,
ArrayList<HashMap<String, String>> attractionList) {
this.data = attractionList;
this.context = context;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(data != null)
return data.size();
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_row2, null);
}
TextView Name = (TextView)convertView.findViewById(R.id.Name); // name
TextView Address = (TextView)convertView.findViewById(R.id.Address); // address
TextView Phone = (TextView)convertView.findViewById(R.id.Phone); // phone
ImageView PicPath = (ImageView)convertView.findViewById(R.id.list_image); // thumb image
HashMap<String, String> attraction = new HashMap<String, String>();
attraction = data.get(position);
Log.i("JSON", attraction.toString());
// Setting all values in listview
Name.setText(attraction.get(KaranganyarAttractionFragment.TAG_NAME));
Address.setText(attraction.get(KaranganyarAttractionFragment.TAG_ADDRESS));
Phone.setText(attraction.get(KaranganyarAttractionFragment.TAG_PHONE));
Log.i("JSON", attraction.get(KaranganyarAttractionFragment.TAG_NAME));
imageLoader.DisplayImage(imageBaseDirectory+attraction.get(KaranganyarAttractionFragment.TAG_PICPATH), position, PicPath);
return convertView;
}
}
KaranganyarAttractionDetailFragment
public class KaranganyarAttractionDetailFragment extends Fragment { //获取联系人JSON的URL // JSON节点名称 public static final String TAG_ID =&#34; ID&#34 ;; public static final String TAG_NAME =&#34; Name&#34 ;; public static final String TAG_ADDRESS =&#34; Address&#34 ;; public static final String TAG_PHONE =&#34; Phone&#34 ;; public static final String TAG_CITY =&#34; City&#34 ;; public static final String TAG_DESCRIPTION =&#34; Description&#34 ;; public static final String TAG_AKSESBILITAS =&#34; Aksesbilitas&#34 ;; public static final String TAG_Tiket =&#34; Tiket&#34 ;; public static final String TAG_FACILITIES =&#34; Faclilities&#34 ;; public static final String TAG_LANTITUDE =&#34; Lantitude&#34 ;; public static final String TAG_LONGTITUDE =&#34; Longtitude&#34 ;; public static final String TAG_PICPATH =&#34; PicPath&#34 ;;
// Hashmap for ListView
ArrayList<HashMap<String, String>> attractionList;
ListView list;
KaranganyarJsonAttractionDetailAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_attraction_karanganyar_detail, container, false);
// Calling async task to get json
new LoadTask().execute();
return rootView;
}
public void loadAttraction(){
attractionList = new
ArrayList<HashMap<String, String>>();
JSONParser parser = new JSONParser();
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {
JSONArray attraction = parser.makeHttpRequestArray("http://plesirsolo.meximas.com/plesir/attraction/wisata/karanganyar/", "GET", params);
// JSONArray attraction = parser.makeHttpRequestArray("http://192.168.56.1:8080/examples/json.html", "GET", params);
// looping through All messages
if(attraction!=null){
for (int i = 0; i < attraction.length(); i++) {
JSONObject c = attraction.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String nama = c.getString(TAG_NAME);
String address = c.getString(TAG_ADDRESS);
String phone = c.getString(TAG_PHONE);
String picPath = c.getString(TAG_PICPATH);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, nama);
map.put(TAG_ADDRESS, address);
map.put(TAG_PHONE, phone);
map.put(TAG_PICPATH, picPath);
// adding HashList to ArrayList
attractionList.add(map);
}
adapter = new KaranganyarJsonAttractionDetailAdapter(
getActivity(), attractionList);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private class LoadTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
loadAttraction();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
list.setAdapter(adapter);
Log.i("JSON", "TEST");
Log.i("JSON", attractionList.toString());
}
}
}
KaranganyarJsonAttractionDetailAdapter
public class KaranganyarJsonAttractionDetailAdapter扩展BaseAdapter {
private ArrayList<HashMap<String, String>> data;
public ImageLoader imageLoader;
private Context context;
public String imageBaseDirectory = "http://plesirsolo.meximas.com/plesir/images/wisata/";
public KaranganyarJsonAttractionDetailAdapter(Context context,
ArrayList<HashMap<String, String>> attractionList) {
this.data = attractionList;
this.context = context;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(data != null)
return data.size();
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.fragment_attraction_karanganyar_detail, null);
}
TextView Name = (TextView)convertView.findViewById(R.id.Name); // name
TextView Address = (TextView)convertView.findViewById(R.id.Address); // address
TextView Phone = (TextView)convertView.findViewById(R.id.Phone); // phone
ImageView PicPath = (ImageView)convertView.findViewById(R.id.list_image); // thumb image
HashMap<String, String> attraction = new HashMap<String, String>();
attraction = data.get(position);
Log.i("JSON", attraction.toString());
// Setting all values in listview
Name.setText(attraction.get(KaranganyarAttractionDetailFragment.TAG_NAME));
Address.setText(attraction.get(KaranganyarAttractionDetailFragment.TAG_ADDRESS));
Phone.setText(attraction.get(KaranganyarAttractionDetailFragment.TAG_PHONE));
Log.i("JSON", attraction.get(KaranganyarAttractionDetailFragment.TAG_NAME));
imageLoader.DisplayImage(imageBaseDirectory+attraction.get(KaranganyarAttractionDetailFragment.TAG_PICPATH), position, PicPath);
return convertView;
}
}
清单
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.yeftaandrea.plesirsolo.SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.yeftaandrea.plesirsolo.MainActivity"
android:label="@string/app_name" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 0 :(得分:0)
你最容易忘记的事情。
<activity
android:name=".KaranganyarAttractionDetailFragment">
</activity>
将代码复制并粘贴到清单中。
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.yeftaandrea.plesirsolo.SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".KaranganyarAttractionDetailFragment">
</activity>
<activity
android:name="com.yeftaandrea.plesirsolo.MainActivity"
android:label="@string/app_name" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
使用此方法只需为应用程序添加名称。
<application
android:name="com.yeftaandrea.plesirsolo"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
答案 2 :(得分:-1)
导航到项目目录中的AndroidManifest.xml文件。将此行放在<application></application>
标记之间(应该有类似的标记)。
<activity android:name=".KaranganyarAttractionDetailFragment"></activity>