当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Fatal exception : IllegalStateException Could not execute method of the activity

问题描述:

Here is my MainActivity class where getUserYouTubeFeed method is called on clicking a Button.

public class MainActivity extends Activity {

// A reference to our list that will hold the video details

private VideosListView listView;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

listView = (VideosListView) findViewById(R.id.videosListView);

}

// This is the XML onClick listener to retreive a users video feed

public void getUserYouTubeFeed(View v){

// We start a new task that does its work on its own thread

// We pass in a handler that will be called when the task has finished

// We also pass in the name of the user we are searching YouTube for

new GetYouTubeUserVideosTask(responseHandler, "done").run();

}

// This is the handler that receives the response when the YouTube task has finished

Handler responseHandler = new Handler() {

public void handleMessage(Message msg) {

populateListWithVideos(msg);

};

};

/**

* This method retrieves the Library of videos from the task and passes them to our ListView

* @param msg

*/

private void populateListWithVideos(Message msg) {

// Retreive the videos are task found from the data bundle sent back

Library lib = (Library) msg.getData().get(GetYouTubeUserVideosTask.LIBRARY);

// Because we have created a custom ListView we don't have to worry about setting the adapter in the activity

// we can just call our custom method with the list of items we want to display

listView.setVideos(lib.getVideos());

}

@Override

protected void onStop() {

// Make sure we null our handler when the activity has stopped

// because who cares if we get a callback once the activity has stopped? not me!

responseHandler = null;

super.onStop();

}

}

Logcat error

08-31 11:34:00.011 27400-27400/com.example.e6530.youtube_playllist E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.e6530.youtube_playllist, PID: 27400

java.lang.IllegalStateException: Could not execute method of the activity

at android.view.View$1.onClick(View.java:4033)

at android.view.View.performClick(View.java:4793)

at android.view.View$PerformClick.run(View.java:19971)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Looper.loop(Looper.java:135)

at android.app.ActivityThread.main(ActivityThread.java:5669)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Caused by: java.lang.reflect.InvocationTargetException

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at android.view.View$1.onClick(View.java:4028)

at android.view.View.performClick(View.java:4793)

at android.view.View$PerformClick.run(View.java:19971)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:135)

at android.app.ActivityThread.main(ActivityThread.java:5669)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Caused by: android.os.NetworkOnMainThreadException

at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1168)

at java.net.InetAddress.lookupHostByName(InetAddress.java:418)

at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)

at java.net.InetAddress.getAllByName(InetAddress.java:215)

at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)

at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)

at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)

at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:604)

at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)

at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)

at com.example.e6530.youtube_playllist.GetYouTubeUserVideosTask.run(GetYouTubeUserVideosTask.java:54)

at com.example.e6530.youtube_playllist.MainActivity.getUserYouTubeFeed(MainActivity.java:37)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at android.view.View$1.onClick(View.java:4028)

at android.view.View.performClick(View.java:4793)

at android.view.View$PerformClick.run(View.java:19971)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:135)

at android.app.ActivityThread.main(ActivityThread.java:5669)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

In above code, I'm trying to get the playlist videos from Youtube then showing thumbnail & Url in list using android listview. Now I'm getting IllegalStateException on the click of button.

网友答案:

Run your Task in AsyncTask

The exception is due to calling network thread on main ui thread

So execute like

     AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
           // your function calling network thread
         }
     });
网友答案:

Instead of trying with Handler Object:

 new GetYouTubeUserVideosTask(responseHandler, "done").run();

Try with Async Task Object:

 new GetYouTubeUserVideosTask(responseHandler, "done").execute();

Note : As Network operation will try to run on main thread and already this main thread is used by UI. So avoid running in Mainthread by using async task/intent service.

For more on Handler and Async Task Link 1

For more on Handler and Async Task Link 2

For more on Intent Service

分享给朋友:
您可能感兴趣的文章:
随机阅读: