错误:在oncreate()之前,系统服务不可用于活动

时间:2014-01-14 21:13:26

标签: android rss oncreate rss-reader

我正在尝试开发一个RSS Reader应用程序。但是我收到错误,下面给出了完整的堆栈跟踪。

01-14 21:18:54.262: E/AndroidRuntime(2541): FATAL EXCEPTION: main
01-14 21:18:54.262: E/AndroidRuntime(2541): java.lang.IllegalStateException: System services not available to Activities before onCreate()
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.app.Activity.getSystemService(Activity.java:4463)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at com.example.simplerssreader.SimpleRssReaderActivity.setData(SimpleRssReaderActivity.java:92)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at com.example.simplerssreader.asyntask.AsynTaskRequest.onPostExecute(AsynTaskRequest.java:98)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at com.example.simplerssreader.asyntask.AsynTaskRequest.onPostExecute(AsynTaskRequest.java:1)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.os.AsyncTask.finish(AsyncTask.java:631)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.os.Looper.loop(Looper.java:137)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at java.lang.reflect.Method.invoke(Method.java:511)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-14 21:18:54.262: E/AndroidRuntime(2541):     at dalvik.system.NativeStart.main(Native Method)

这是我的主要活动

public class SimpleRssReaderActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_rss_reader);
        new AsynTaskRequest().execute();        
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.simple_rss_reader, menu);
        return true;
    }
    public  void setData(){     
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, AsynTaskRequest.headLines);
        setListAdapter(adapter);        
    }

}

AsynTask类代码

public class AsynTaskRequest extends AsyncTask<Void, Void, String>{ 
    public static List<String> headLines;
    public static List<String> links;
    SimpleRssReaderActivity smrssreader;    

    @Override
    protected String doInBackground(Void... params) {
        try {
            headLines = new ArrayList<String>();
             links    = new ArrayList<String>();
            URL url = new URL("http://gcuf.edu.pk/feed/");

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();

                // We will get the XML from an input stream
            xpp.setInput(getInputStream(url), "UTF_8");         
            boolean insideItem = false;

            //Retruns the type of current event: START_TAG, END_TAG, etc..
            int eventType= xpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {

                if (eventType == XmlPullParser.START_TAG) {
                    if (xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = true;                      
                    }else if (xpp.getName().equalsIgnoreCase("title")) {
                        if(insideItem){
                            headLines.add(xpp.nextText()); // Extract the headline
                        }                           
                    }else if (xpp.getName().equalsIgnoreCase("link")) {
                        if (insideItem) {
                            links.add(xpp.nextText()); // Extract the link of article
                        }
                    }
                }else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = false;                 
                }
                eventType = xpp.next();
                }           
        } catch (MalformedURLException e) {
                e.printStackTrace();
        } catch (XmlPullParserException e){
            e.printStackTrace();            
        }catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public InputStream getInputStream (URL url){
        try {
            return url.openConnection().getInputStream();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result) {   
        super.onPostExecute(result);
        smrssreader = new SimpleRssReaderActivity();
        smrssreader.setData();

    }

}

1 个答案:

答案 0 :(得分:1)

您的onPostExecute()错了。 从不自己创建活动的实例,更不用说通过构造函数了。相反,您的AsynTaskRequest需要有一些与其活动进行通信的方式。由于用户旋转屏幕或其他一些配置更改,它还需要处理活动在此工作进行时可能会发生变化的事实。

你可以:

  • 使用事件总线(LocalBroadcastManager,Otto,greenrobot的EventBus等),并onPostExecute()引发活动在工作结束后接收的事件,或

  • AsyncTask移动为保留片段的内部类,因此该任务适用于onPostExecute()中的片段,并且片段知道要与之交谈的正确活动,或者< / p>

存在其他模式,但开始时只有两种模式。