Reputation: 33
I've searched through quite a few posts, and couldn't find quite my error. I am attempting to get an example RSS feed reader working to understand how it works. If anyone has a better example, feel free to link. Thank you for your time. 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
Actual code
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();
}
}
There are several more .java files, but this is the one that is throwing errors, along with
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;
}
}
Alright, did number 3, as suggested. New errors, posting code and log cat. Much appreciated for the fast response!
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
Upvotes: 3
Views: 524
Reputation: 86948
The important information from this LogCat is:
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'
Explanation
MainActivity
cannot find the method parseRss()
because it is in the ReadRssFeed
class.
You are passing the same layout to both Activities, which is a little confusing... But inside activity_main.xml
you use the attribute android:onClick="parseRss"
. android:onClick
requires that the name provided relate to a method in the Activity that displays the layout. As I said before MainActivity
doesn't have parseRss()
, hence the error.
Possible Solutions
MainActivity
(without android:onClick="parseRss"
) and another to ReadRssFeed
OrparseRss()
to MainActivity
in some way, shape, or form OrMainActivity
currently does nothing, you could delete it and rename ReadRssFeed
to MainActivity
...Reading a LogCat
Rather than read each logcat for you and walk you step by step to a running application (which isn't the purpose of this forum.) I'll teach you how to read most logcats so you can solve many of these problems yourself.
Find the lowest "Caused by..."
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
Which roughly means that you are trying to cast an object into something that it cannot become. We keep our eye out for code working with Application objects.
Work down the logcat, line-by-line, until you encounter code from one of your classes. (Here it is the first line.)
10-26 12:53:27.274: E/AndroidRuntime(539): at com.example.example.android.Rssfeed.MainActivity.onCreate(MainActivity.java:36)
This tells you the package, class, and method where the code failed. The most useful portion is the end MainActivity.java:36
, this number is the exact line number where the error occurred.
Go to line 36 in MainActivity (use Ctrl+L in Eclipse). This line is the culprit:
RssAplication application = (RssAplication) getApplication();
However this is where my usefulness stops. You haven't posted your RssAplication class so I cannot see what is happening. My first question is: does RssAplication extend Application?
If this answer solved your original question please click the checkmark. Also it's nice to click the up arrow on any answer that you find useful.
If you encounter any more problems that you just cannot solve, please ask a new question. Good luck!
Upvotes: 2