我搜索过很多帖子,但找不到我的错误。我试图让一个示例RSS提要阅读器工作,以了解它是如何工作的。如果有人有更好的例子,请随时链接。感谢您的时间。 logcat的:
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位。新错误,发布代码和日志cat。非常感谢快速响应!
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();
}
}
logcat的:
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
答案 0 :(得分: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"
。 android:onClick
要求所提供的名称与显示布局的Activity中的方法相关。正如我之前所说MainActivity
没有parseRss()
,因此错误。
可能的解决方案
MainActivity
(不android:onClick="parseRss"
),另一个针对ReadRssFeed
或 parseRss()
或 MainActivity
目前无效,您可以将其删除并将MainActivity
重命名为ReadRssFeed
... 阅读LogCat
不是为你读取每个logcat而是一步一步地走向正在运行的应用程序(这不是本论坛的目的。)我将教你如何阅读大多数logcats,以便你可以解决许多这些问题你自己。
找到最低的“引起......”
MainActivity
这大致意味着您正在尝试将对象转换为无法成为的对象。我们密切关注使用Application对象的代码。
逐行处理logcat,直到遇到其中一个类的代码。 (这是第一行。)
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)
,此数字是发生错误的确切行号。
转到MainActivity中的第36行(在Eclipse中使用 Ctrl + L )。这条线是罪魁祸首:
MainActivity.java:36
然而,这是我的用处停止的地方。你还没有发布你的RssAplication类,所以我看不到发生了什么。我的第一个问题是:RssAplication是否扩展了应用程序?
如果此答案解决了您的原始问题,请点击复选标记。此外,单击任何有用的答案上的向上箭头也很不错。
如果您遇到任何您无法解决的问题,请提出新问题。祝你好运!