致命异常:IllegalStateException无法执行活动的方法

时间:2016-08-31 06:46:16

标签: android listview android-fragments youtube

这是我的MainActivity类,其中在单击Button时调用getUserYouTubeFeed方法。

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错误

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) 

在上面的代码中,我试图从Youtube获取播放列表视频,然后显示缩略图和&使用android listview列表中的url。现在,我点击按钮即可获得IllegalStateException

2 个答案:

答案 0 :(得分:1)

而不是尝试使用处理程序对象:

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

尝试使用异步任务对象:

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

注意:由于网络操作将尝试在主线程上运行,并且UI已使用此主线程。因此,请避免使用异步任务/意图服务在Mainthread中运行。

有关Handler and Async Task Link 1

的更多信息

有关Handler and Async Task Link 2

的更多信息

有关Intent Service

的更多信息

答案 1 :(得分:0)

AsyncTask

中运行您的任务

异常是由于在主ui线程上调用网络线程

执行类似

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