0

我正在开发一个从 Web 服务解析 JSON 数据的应用程序。然后将此数据存储在列表视图中,我正在尝试启用应用程序以允许我单击列表中的项目。反过来,通过单击我试图将活动从列表视图切换到 Web 视图的项目。但是,当我单击列表中的任何项目时,应用程序指出发生了异常,显示一条消息,指出“不幸的是 [应用程序名称] 已停止。”,然后退出应用程序。

除了这个之外,应用程序中没有其他问题。因此,如果有人可以帮助我解决此问题,我将不胜感激。我提供了主要活动的全部代码,问题发生在 onCreate 的 try catch 中,您也可以从这里下载应用程序:http: //bit.ly/17rePw0

public class MainActivity extends ListActivity 
{   
    GPSManager gpsManager;

    ConnectionManager connectManager;

    LocationManager locationManager = null;
    LocationListener locationListener = null;

    AlertDialogManager alertManager = new AlertDialogManager();

    private ProgressDialog progDialog;

    JSONParser parser = new JSONParser();

    ArrayList<HashMap<String, String>> articleList;

    private String URL_ARTICLES = "http://api.wikilocation.org/articles?lat=56.4607208&lng=-2.9680668&format=json&radius=100";
    private static final String TAG_ARTICLES = "articles";
    private static final String TAG_ID = "id";
    private static final String TAG_LAT = "lat";
    private static final String TAG_LNG = "lng";
    private static final String TAG_NAME = "title";
    private static final String TAG_URL = "url";
    private static final String TAG_DISTANCE = "distance";

    private static final int ARTICLE_LIMIT = 20;
    private static int ARTICLE_RADIUS = 100;

    JSONArray articles = null;

    double lat, lng;
    String latitude, longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        articleList = new ArrayList<HashMap<String, String>>();

        connectManager = new ConnectionManager(getApplicationContext());
        gpsManager = new GPSManager(this);

        if(gpsManager.RetrieveLocation)
        {
            lat = gpsManager.RetrieveLatitude();
            lng = gpsManager.RetrieveLongitude();

            latitude = String.valueOf(lat);
            longitude = String.valueOf(lng);

            //URL_ARTICLES = "http://api.wikilocation.org/articles?lat=" + latitude + "&lng=" + longitude + "&format=json&radius=100";
        }
        else
        {
            gpsManager.showSettingsAlert();
        }

        new LoadArticles().execute();

        ListView listView = getListView();

        try
        {
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapt, View view, int position, long id) {
                    Intent intent = new Intent(MainActivity.this, WebActivity.class);
                    startActivity(intent);
                }      
            });
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    class LoadArticles extends AsyncTask<String, String, String> 
    {
        protected void onPreExecute()
        {
            super.onPreExecute();

            progDialog = new ProgressDialog(MainActivity.this);
            progDialog.setMessage("Loading Articles");
            progDialog.setIndeterminate(false);
            progDialog.setCancelable(false);
            progDialog.show();
        }

        protected String doInBackground(String... args)
        {
            List<NameValuePair>params = new ArrayList<NameValuePair>();

            String json = parser.makeHttpRequest(URL_ARTICLES, "GET", params);

            Log.d("Articles JSON: ", "> " + json);

            try
            {
                JSONObject jObj = new JSONObject(json);

                if(jObj != null)
                {
                    articles = jObj.getJSONArray(TAG_ARTICLES);

                    for(int i = 0; i < articles.length(); i++)
                    {
                        JSONObject obj = articles.getJSONObject(i);

                        String id = obj.getString(TAG_ID);
                        String lat = obj.getString(TAG_LAT);
                        String lng = obj.getString(TAG_LNG);
                        String name = obj.getString(TAG_NAME);
                        String address = obj.getString(TAG_URL);
                        String distance = obj.getString(TAG_DISTANCE);

                        HashMap<String, String>map = new HashMap<String, String>();

                        map.put(TAG_ID, id);
                        map.put(TAG_LAT, lat);
                        map.put(TAG_LNG, lng);
                        map.put(TAG_NAME, name);
                        map.put(TAG_URL, address);
                        map.put(TAG_DISTANCE, distance);

                        articleList.add(map);
                    }
                }
            }
            catch(JSONException e)
            {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url)
        {
            progDialog.dismiss();

            runOnUiThread(new Runnable(){

                public void run()
                {
                    ListAdapter adapter = new SimpleAdapter(MainActivity.this, articleList, 
                            R.layout.list_item_articles, new String[]
                    {
                        TAG_ID, TAG_NAME, TAG_DISTANCE }, new int[]{ R.id.artcile_id, R.id.article_name, R.id.article_distance
                    });

                    setListAdapter(adapter);
                }
            });
        }
    }
}

编辑:添加了 LogCat 堆栈跟踪。

08-03 23:44:02.407: W/dalvikvm(875): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-03 23:44:02.457: E/AndroidRuntime(875): FATAL EXCEPTION: main
08-03 23:44:02.457: E/AndroidRuntime(875): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.muc2013/com.android.muc2013.WebActivity}; have you declared this activity in your AndroidManifest.xml?
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.app.Activity.startActivityForResult(Activity.java:3370)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.app.Activity.startActivityForResult(Activity.java:3331)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.app.Activity.startActivity(Activity.java:3566)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.app.Activity.startActivity(Activity.java:3534)
08-03 23:44:02.457: E/AndroidRuntime(875):  at com.android.muc2013.MainActivity$1.onItemClick(MainActivity.java:99)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.os.Handler.handleCallback(Handler.java:725)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.os.Looper.loop(Looper.java:137)
08-03 23:44:02.457: E/AndroidRuntime(875):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-03 23:44:02.457: E/AndroidRuntime(875):  at java.lang.reflect.Method.invokeNative(Native Method)
08-03 23:44:02.457: E/AndroidRuntime(875):  at java.lang.reflect.Method.invoke(Method.java:511)
08-03 23:44:02.457: E/AndroidRuntime(875):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-03 23:44:02.457: E/AndroidRuntime(875):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-03 23:44:02.457: E/AndroidRuntime(875):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

您错过了在 manifest.xml 中声明另一个活动

所以Android risis这个异常

找不到明确的活动类 {com.android.muc2013/com.android.muc2013.WebActivity}; 您是否在 AndroidManifest.xml 中声明了此活动?

在 manifest.xml 文件中声明此活动

<activity android:name="com.android.muc2013.WebActivity"/>
于 2013-08-04T04:24:19.527 回答