提问者:小点点

通过广播接收器发送数据-在无法在线程内创建处理程序时崩溃


我在Android中很新,所以请耐心等待:)

我正在尝试通过广播接收器将数据从解析器活动发送到UI活动,在那里我想显示数据。

当我运行我的应用程序时,它会在“无法在未调用Loop.准备()的线程中创建处理程序”上崩溃

我把我的代码放在这里,这样你就可以帮助我了。

非常感谢:)

      public class HandlePush extends Activity implements OnClickListener{

    Button mButton;
    TextView mSignal;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handlepush);

        mSignal=(TextView)findViewById(R.id.TodaySigTV);


        registerReceiver(myBroadCastReceiver, new IntentFilter("my.action.string"));

        mButton=(Button)findViewById(R.id.SignalButton);
        mButton.setOnClickListener(this);

        //Get a Tracker (should auto-report)
        ((AppManager) getApplication()).getTracker(AppManager.TrackerName.APP_TRACKER);
    }//oncreate



    private BroadcastReceiver myBroadCastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            Log.i("Receiver", "Broadcast received: " + action);
            if(action.equals("my.action.string")){
                String state = intent.getExtras().getString("extra");
                mSignal.setText(state);

            }
        }

    };


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        //Get an Analytics tracker to report app starts & uncaught exceptions etc.
        GoogleAnalytics.getInstance(this).reportActivityStart(this);
    }


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

        //Stop the analytics tracking
        GoogleAnalytics.getInstance(this).reportActivityStop(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(getApplicationContext(), Contact.class);
        startActivity(intent);
        finish();
    }

}//handlepush


public class HistoryParser extends Activity{
// We don't use namespaces
        private final String ns = null;
        HistoryItems mObject;
        List<HistoryItems>mList;


        public List<HistoryItems> parse(InputStream inputStream) throws XmlPullParserException, IOException {
            try {
                XmlPullParser parser = Xml.newPullParser();
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(inputStream, null);
                parser.nextTag();
                return readFeed(parser);
            } finally {
                inputStream.close();
            }
        }


        private List<HistoryItems> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
            List<HistoryItems> entries = new ArrayList<HistoryItems>();

            parser.require(XmlPullParser.START_TAG, ns, "rss");
            while (parser.next() != XmlPullParser.END_DOCUMENT) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                // Starts by looking for the entry tag
                if (name.equals("item")) {
                    entries.add(readEntry(parser));
                } else {
                    //                  skip(parser);
                }
            }  
            return entries;
        }


        private HistoryItems readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
            parser.require(XmlPullParser.START_TAG, ns, "item");
            String title = null;
            String dateStr = null;

            while (parser.next() != XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                if (name.equals("title")) {
                    title = readTitle(parser);
                //  sendData(title);////send the data from here
                } else if (name.equals("pubDate")) {
                    dateStr = readDate(parser);

                } else {
                    skip(parser);
                }
            } 
        //  sendData(title);////send the data from here
            return new HistoryItems(title, dateStr);

        }

        private String readTitle(XmlPullParser parser) throws XmlPullParserException, IOException {
            parser.require(XmlPullParser.START_TAG, ns, "title");
            String title = readText(parser);
            parser.require(XmlPullParser.END_TAG, ns, "title");
            sendData(title);////send the data from here
            return title;
        }

        private String readDate(XmlPullParser parser) throws XmlPullParserException, IOException{
            parser.require(XmlPullParser.START_TAG, ns, "pubDate");
            String date = readText(parser);
            parser.require(XmlPullParser.END_TAG, ns, "pubDate");
            return date;
        }

        // For the tags title and link, extract their text values.
        private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
            String result = "";
            if (parser.next() == XmlPullParser.TEXT) {
                result = parser.getText();
                parser.nextTag();
            }
            return result;
        }

        private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                throw new IllegalStateException();
            }
            int depth = 1;
            while (depth != 0) {
                switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    break;
                }
            }
        }

        public void sendData(String title){

        Intent intent = new Intent("my.action.string");
        intent.putExtra("extra", title); // extra is the sent title
        sendBroadcast(intent);

        }

}//历史解析器


共1个答案

匿名用户

您不能将广播接收器放在AsynTask中。BroadCastReciver应该在Activity中,而在onCreate中,您应该注册reiver和action,如下所示:

注册接收器(yourRec的,新的IntentFilter(yourAction));

希望对你有帮助!!

编辑:

您应该像属性一样在Activity(AsynTask之外)中声明您的广播接收器。类似于这样:

private BroadcastReceiver myBroadCastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                String action = intent.getAction();
                Log.i("Receiver", "Broadcast received: " + action);
                if(action.equals("my.action.string")){
                    String state = intent.getExtras().getString("extra");
                    mSignal.setText(state);

                }
            }

        }