3

我已经搜索了很多帖子,但找不到我的错误。我正在尝试获取一个示例 RSS 提要阅读器,以了解它是如何工作的。如果有人有更好的例子,请随时链接。感谢您的时间。日志猫:

10-24 16:06:47.360: I/ActivityManager(91): Start proccom.example.example.android.Rssfeed for activity com.example.example.android.Rssfeed/.MainActivity: pid=544 uid=10041 gids={3003}        
10-24 16:06:47.390: I/WindowManager(91): createSurface Window{413cf2f8 Starting com.example.example.android.Rssfeed paused=false}: DRAW NOW PENDING
10-24 16:06:47.551: W/NetworkManagementSocketTagger(91): setKernelCountSet(10041, 1) failed with errno -2
10-24 16:06:48.370: I/WindowManager(91): createSurface Window{4163f540 com.example.example.android.Rssfeed/com.example.example.android.Rssfeed.MainActivity paused=false}: DRAW NOW PENDING
10-24 16:06:48.450: D/gralloc_goldfish(544): Emulator without GPU emulation detected.
10-24 16:06:48.520: I/ActivityManager(91): Displayed com.example.example.android.Rssfeed/.MainActivity: +1s257ms
10-24 16:06:48.940: W/InputManagerService(91): Starting input on non-focused client   com.android.internal.view.IInputMethodClient$Stub$Proxy@415c36a8 (uid=10009 pid=352)
10-24 16:06:48.980: D/dalvikvm(186): GC_CONCURRENT freed 64K, 15% free 13713K/16071K, paused 33ms+15ms
10-24 16:06:49.079: W/NetworkManagementSocketTagger(91): setKernelCountSet(10009, 0) failed with errno -2
10-24 16:06:49.929: D/dalvikvm(186): GC_FOR_ALLOC freed 1375K, 16% free 13617K/16071K, paused 54ms
10-24 16:06:50.049: D/dalvikvm(186): GC_CONCURRENT freed 647K, 19% free 13061K/16071K, paused 5ms+11ms
10-24 16:06:50.439: D/AndroidRuntime(544): Shutting down VM
10-24 16:06:50.439: W/dalvikvm(544): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
10-24 16:06:50.459: E/AndroidRuntime(544): FATAL EXCEPTION: main
10-24 16:06:50.459: E/AndroidRuntime(544): java.lang.IllegalStateException: Could not find a method parseRss(View) in the activity class com.example.example.android.Rssfeed.MainActivity for onClick handler on view class android.widget.Button with id 'readRss'
10-24 16:06:50.459: E/AndroidRuntime(544):  at android.view.View$1.onClick(View.java:3026)
10-24 16:06:50.459: E/AndroidRuntime(544):  at android.view.View.performClick(View.java:3480)
10-24 16:06:50.459: E/AndroidRuntime(544):  at android.view.View$PerformClick.run(View.java:13983)
10-24 16:06:50.459: E/AndroidRuntime(544):  at android.os.Handler.handleCallback(Handler.java:605)
10-24 16:06:50.459: E/AndroidRuntime(544):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 16:06:50.459: E/AndroidRuntime(544):  at android.os.Looper.loop(Looper.java:137)
10-24 16:06:50.459: E/AndroidRuntime(544):  at android.app.ActivityThread.main(ActivityThread.java:4340)
10-24 16:06:50.459: E/AndroidRuntime(544):  at java.lang.reflect.Method.invokeNative(Native Method)
10-24 16:06:50.459: E/AndroidRuntime(544):  at java.lang.reflect.Method.invoke(Method.java:511)
10-24 16:06:50.459: E/AndroidRuntime(544):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-24 16:06:50.459: E/AndroidRuntime(544):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-24 16:06:50.459: E/AndroidRuntime(544):  at dalvik.system.NativeStart.main(Native Method)
10-24 16:06:50.459: E/AndroidRuntime(544): Caused by: java.lang.NoSuchMethodException: parseRss [class android.view.View]
10-24 16:06:50.459: E/AndroidRuntime(544):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-24 16:06:50.459: E/AndroidRuntime(544):  at  java.lang.Class.getMethod(Class.java:915)
10-24 16:06:50.459: E/AndroidRuntime(544):  at android.view.View$1.onClick(View.java:3019)
10-24 16:06:50.459: E/AndroidRuntime(544):  ... 11 more
10-24 16:06:50.501: W/ActivityManager(91):   Force finishing activity com.example.example.android.Rssfeed/.MainActivity
10-24 16:06:50.519: W/WindowManager(91): Failure taking screenshot for (180x300) to layer 21015
10-24 16:06:50.639: I/WindowManager(91): createSurface Window{415c26e8  paused=false}: DRAW NOW PENDING
10-24 16:06:50.889: I/ARMAssembler(36): generated scanline__00000077:03515104_00008001_00000000 [113 ipp] (134 ins) at [0x41c99450:0x41c99668] in 797599 ns
10-24 16:06:51.033: W/ActivityManager(91): Activity pause timeout for ActivityRecord{414aae20 com.example.example.android.Rssfeed/.MainActivity}
10-24 16:06:51.059: W/NetworkManagementSocketTagger(91): setKernelCountSet(10009, 1) failed with errno -2
10-24 16:06:51.129: D/AccountManagerService(91): bind attempt failed for Session: expectLaunch true, connected false, stats (0/0/0), lifetime 0.001, addAccount, accountType com.google, requiredFeatures null
10-24 16:06:51.159: I/WindowManager(91): createSurface Window{414b2ff0 com.android.contacts/com.android.contacts.activities.PeopleActivity paused=false}: DRAW NOW PENDING
10-24 16:06:51.219: E/AccountPromptUtils(352): Account setup error: Authenticator experienced an I/O problem
10-24 16:06:51.879: W/NetworkManagementSocketTagger(91): setKernelCountSet(10041, 0) failed with errno -2
10-24 16:06:51.879: I/dalvikvm(91): Jit: resizing JitTable from 4096 to 8192
10-24 16:06:53.709: I/Process(544): Sending signal. PID: 544 SIG: 9
10-24 16:06:54.029: I/ActivityManager(91): Process com.example.example.android.Rssfeed (pid 544) has died.
10-24 16:06:54.029: I/WindowManager(91): WIN DEATH: Window{4163f540 com.example.example.android.Rssfeed/com.example.example.android.Rssfeed.MainActivity paused=false}
10-24 16:07:01.924: W/ActivityManager(91): Activity destroy timeout for ActivityRecord{414aae20 com.example.example.android.Rssfeed/.MainActivity}
10-24 16:07:18.002: D/ExchangeService(438): Received deviceId from Email app: androidc259148960
10-24 16:07:18.002: D/ExchangeService(438): Reconciling accounts...
10-24 16:07:30.529: W/ThrottleService(91): unable to find stats for iface rmnet0
10-24 16:09:00.190: D/dalvikvm(91): GC_CONCURRENT freed 480K, 9% free 12125K/13319K, paused 6ms+10ms
10-24 16:11:40.999: D/dalvikvm(171): GC_CONCURRENT freed 456K, 6% free 10349K/10951K, paused 5ms+5ms
10-24 16:17:30.540: W/ThrottleService(91): unable to find stats for iface rmnet0: E/(): Device disconnected

实际代码

package com.example.example.android.Rssfeed;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ReadRssFeed extends ListActivity {
    private ParseTask parseTask;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        // .detectAll().penaltyLog().penaltyDeath().build());
        // StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
        // .penaltyLog().penaltyDeath().build());

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parseTask = (ParseTask) getLastNonConfigurationInstance();
        if (parseTask != null) {
            parseTask.setActivity(this);
        }
        ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this,
                R.layout.rowlayout, android.R.id.text1, RssAplication.list);
        RssAplication application = (RssAplication) getApplication();
        setListAdapter(adapter);
    }

    public void parseRss(View view) {
        Log.e("DEBUG", "parseRss");
        if (parseTask == null) {
            parseTask = new ParseTask(this);
            parseTask
                    .execute(new String[] { "http://www.beaufortcountynow.com/rss.php" });
        }

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show();
    }

    private static class ParseTask extends
            AsyncTask<String, Void, List<RssItem>> {
        private ReadRssFeed activity;

        private ParseTask(ReadRssFeed activity) {
            setActivity(activity);
        }

        public void setActivity(ReadRssFeed activity) {
            this.activity = activity;
            Log.d("DEBUG", "ParseTask's activity: " + activity);
        }

        @Override
        protected List<RssItem> doInBackground(String... params) {
            Log.d("DEBUG", "doInBackground");
            return FeedReader.parse(params[0], activity);
        }

        @Override
        protected void onPostExecute(List<RssItem> list) {
            Log.w("DEBUG", "onPostExecute called");
            finishWithText(list);
        }

        @SuppressLint("NewApi")
        private void finishWithText(List<RssItem> list) {
            String text = String.valueOf(list.size());
            Log.d("DEBUG", "ParseTask done. Updating activity: " + activity
                    + ", message: " + text);
            if (activity != null) {
                activity.parseTask = null;

                TextView textView = (TextView) activity
                        .findViewById(R.id.textViewCount);
                textView.setText(text);
                @SuppressWarnings("unchecked")
                ArrayAdapter<RssItem> adapter = (ArrayAdapter<RssItem>) activity
                        .getListAdapter();
                adapter.clear();
                adapter.addAll(list);
                RssAplication.list = list;
            }
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        if (parseTask != null) {
            return parseTask;
        }
        return super.onRetainNonConfigurationInstance();
    }

    @Override
    protected void onDestroy() {
        if (parseTask != null) {
            parseTask.setActivity(null);
        }
        super.onDestroy();
    }
}

还有几个 .java 文件,但这是一个引发错误的文件,以及

package com.example.example.android.Rssfeed;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

好的,按照建议进行了第 3 次。新错误、发布代码和日志猫。非常感谢您的快速响应!

 package com.example.example.android.Rssfeed;

  import java.util.List;

 import android.annotation.SuppressLint;
 import android.app.ListActivity;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.Toast;

 public class MainActivity extends ListActivity {

private ParseTask parseTask;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    // StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    // .detectAll().penaltyLog().penaltyDeath().build());
    // StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
    // .penaltyLog().penaltyDeath().build());

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    parseTask = (ParseTask) getLastNonConfigurationInstance();
    if (parseTask != null) {
        parseTask.setActivity(this);
    }
    ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this,
            R.layout.rowlayout, android.R.id.text1,              RssAplication.list);
    RssAplication application = (RssAplication) getApplication();
    setListAdapter(adapter);
}

public void parseRss(View view) {
    Log.e("DEBUG", "parseRss");
    if (parseTask == null) {
        parseTask = new ParseTask(this);
        parseTask
                .execute(new String[] { "http://www.beaufortcountynow.com/rss.php" });
    }

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show();
}

private static class ParseTask extends
        AsyncTask<String, Void, List<RssItem>> {
    private MainActivity activity;

    private ParseTask(MainActivity activity) {
        setActivity(activity);
    }

    public void setActivity(MainActivity activity) {
        this.activity = activity;
        Log.d("DEBUG", "ParseTask's activity: " + activity);
    }

    @Override
    protected List<RssItem> doInBackground(String... params) {
        Log.d("DEBUG", "doInBackground");
        return FeedReader.parse(params[0], activity);
    }

    @Override
    protected void onPostExecute(List<RssItem> list) {
        Log.w("DEBUG", "onPostExecute called");
        finishWithText(list);
    }

    @SuppressLint("NewApi")
    private void finishWithText(List<RssItem> list) {
        String text = String.valueOf(list.size());
        Log.d("DEBUG", "ParseTask done. Updating activity: " + activity
                + ", message: " + text);
        if (activity != null) {
            activity.parseTask = null;

            TextView textView = (TextView) activity
                    .findViewById(R.id.textViewCount);
            textView.setText(text);
            @SuppressWarnings("unchecked")
            ArrayAdapter<RssItem> adapter = (ArrayAdapter<RssItem>) activity
                    .getListAdapter();
            adapter.clear();
            adapter.addAll(list);
            RssAplication.list = list;
        }
    }
}

@Override
public Object onRetainNonConfigurationInstance() {
    if (parseTask != null) {
        return parseTask;
    }
    return super.onRetainNonConfigurationInstance();
}

@Override
protected void onDestroy() {
    if (parseTask != null) {
        parseTask.setActivity(null);
    }
    super.onDestroy();
}
   }

日志猫:

 10-26 12:53:26.224: D/dalvikvm(539): Not late-enabling CheckJNI (already on)
 10-26 12:53:27.254: D/AndroidRuntime(539): Shutting down VM
 10-26 12:53:27.264: W/dalvikvm(539): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
 10-26 12:53:27.274: E/AndroidRuntime(539): FATAL EXCEPTION: main
 10-26 12:53:27.274: E/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.example.android.Rssfeed/com.example.example.android.Rssfeed.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.example.android.Rssfeed.RssAplication
 10-26 12:53:27.274: E/AndroidRuntime(539):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
  10-26 12:53:27.274: E/AndroidRuntime(539):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at android.os.Handler.dispatchMessage(Handler.java:99)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at android.os.Looper.loop(Looper.java:137)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at android.app.ActivityThread.main(ActivityThread.java:4340)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at java.lang.reflect.Method.invokeNative(Native Method)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at java.lang.reflect.Method.invoke(Method.java:511)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at dalvik.system.NativeStart.main(Native Method)
 10-26 12:53:27.274: E/AndroidRuntime(539): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.example.android.Rssfeed.RssAplication
 10-26 12:53:27.274: E/AndroidRuntime(539):     at com.example.example.android.Rssfeed.MainActivity.onCreate(MainActivity.java:36)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at  android.app.Activity.performCreate(Activity.java:4465)
 10-26 12:53:27.274: E/AndroidRuntime(539):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  10-26 12:53:27.274: E/AndroidRuntime(539):    at      android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
 10-26 12:53:27.274: E/AndroidRuntime(539):     ... 11 more
 10-26 12:58:27.543: I/Process(539): Sending signal. PID: 539 SIG: 9
4

1 回答 1

2

此 LogCat 的重要信息是:

java.lang.IllegalStateException: Could not find a method parseRss(View) 
    in the activity class com.example.example.android.Rssfeed.MainActivity 
    for onClick handler on view class android.widget.Button with id 'readRss'

解释

MainActivity找不到该方法parseRss(),因为它在ReadRssFeed类中。

您将相同的布局传递给两个活动,这有点令人困惑......但是在里面activity_main.xml你使用了属性android:onClick="parseRss"要求提供的名称与 Activity 中显示布局的方法相关。正如我之前所说,没有,因此错误。 android:onClick MainActivityparseRss()

可能的解决方案

  1. 创建两个布局,一个特定于MainActivity(不带android:onClick="parseRss"),另一个用于ReadRssFeed Or
  2. 以某种方式添加parseRss()MainActivity,形状或形式
  3. 由于MainActivity当前什么都不做,您可以将其删除并重命名ReadRssFeedMainActivity...

读取 LogCat

而不是为您阅读每个 logcat 并逐步引导您到正在运行的应用程序(这不是本论坛的目的。)我将教您如何阅读大多数 logcat,以便您自己解决许多这些问题。

  1. 找到最低的“由...引起”

    10-26 12:53:27.274: E/AndroidRuntime(539): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.example.android.Rssfeed.RssAplication
    

    这大致意味着您正在尝试将一个对象投射到它无法成为的东西中。我们密切关注使用 Application 对象的代码。

  2. 逐行处理 logcat,直到遇到来自某个类的代码。(这里是第一行。)

    10-26 12:53:27.274: E/AndroidRuntime(539):     at com.example.example.android.Rssfeed.MainActivity.onCreate(MainActivity.java:36)
    

    这会告诉您代码失败的包、类和方法。最有用的部分是 end MainActivity.java:36,这个数字是发生错误的确切行号。

  3. 转到 MainActivity 中的第 36 行(在 Eclipse中使用Ctrl+ )。L这条线是罪魁祸首:

    RssAplication application = (RssAplication) getApplication();
    

    然而,这就是我的用处停止的地方。你还没有发布你的 RssAplication 课程,所以我看不到发生了什么。我的第一个问题是:RssAplication 是否扩展了应用程序?


如果此答案解决了您的原始问题,请单击复选标记。此外,在您认为有用的任何答案上单击向上箭头也很不错。

如果您遇到更多无法解决的问题,请提出新问题。祝你好运!

于 2012-10-24T17:31:12.013 回答