如何从Fragment启动Activity? Android的

时间:2014-03-17 23:51:20

标签: android eclipse fragment forceclose rss-reader

我是Android应用开发的新手。我使用这个带有片段的开源滑动菜单:http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

我想在滑动菜单项目中实现这个rss feed解析器(AndroidRssReader,也是开源):http://android-er.blogspot.in/2010/04/simple-rss-reader-using-androids.html

我的想法是,一个滑动菜单项(一个片段),启动AndroidRssReader.java活动。我把AndroidRssReader.java文件放在我的/src/stiw47.logos中,并想从RssFragment.java调用em。这是我的RssFragment.java的源代码:

public class RssFragment extends Fragment {

    public RssFragment (){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_rss, container, false);

        Button newPage = (Button)v.findViewById(R.id.bNovosti);
        newPage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AndroidRssReader.class);
                startActivity(intent);
            }
        });
        return v;
    }
}

当我单击一个按钮来调用AndroidRssReader类时,我关闭了一个力。

这是我的adb logcat:

    I/ActivityManager(13216): Timeline: Activity_launch_request id:stiw47.logos time:7317484
    V/AudioPolicyManagerBase( 1678): getOutput() stream 1, samplingRate 0, format0,         channelMask 3, flags 0
V/AudioPolicyManagerBase( 1678): getOutput() returns output 2
V/audio_policy_anm( 1678): audio_io_handle_t android::ap_get_output(audio_policy*, audio_stream_type_t, uint32_t, audio_format_t, uint32_t, audio_output_flags_t): tid 1678
V/AudioPolicyManagerBase( 1678): getOutput() stream 1, samplingRate 0, format 0, channelMask 3, flags 0
V/AudioPolicyManagerBase( 1678): getOutput() returns output 2
V/audio_policy_anm( 1678): audio_io_handle_t android::ap_get_output(audio_policy*, audio_stream_type_t, uint32_t, audio_format_t, uint32_t, audio_output_flags_t): tid 2371
V/AudioPolicyManagerBase( 1678): getOutput() stream 1, samplingRate 48000, format 1, channelMask 3, flags 4
V/AudioPolicyManagerBase( 1678): getOutput() returns output 2
W/AudioTrack( 2143): AUDIO_OUTPUT_FLAG_FAST denied by client due to mismatching sample rate (48000 vs 44100)
I/ActivityManager( 2143): START u0 {cmp=stiw47.logos/.AndroidRssReader} from pid 13216
V/AudioPolicyManagerBase( 1678): startOutput() output 2, stream 1, session 62
V/AudioPolicyManagerBase( 1678): changeRefCount() stream 1, count 1
V/AudioPolicyManagerBase( 1678): getNewDevice() selected device 2
V/AudioPolicyManagerBase( 1678): setOutputDevice() output 2 device 0002 force 0 delayMs 0 
V/AudioPolicyManagerBase( 1678): setOutputDevice() prevDevice 0002
V/AudioPolicyManagerBase( 1678): setOutputDevice() setting same device 0002 or null device for output 2
V/AudioPolicyManagerBase( 1678): releaseOutput() 2
D/AndroidRuntime(13216): Shutting down VM
W/dalvikvm(13216): threadid=1: thread exiting with uncaught exception (group=0x41febce0)
E/AndroidRuntime(13216): FATAL EXCEPTION: main
E/AndroidRuntime(13216): Process: stiw47.logos, PID: 13216
E/AndroidRuntime(13216): java.lang.RuntimeException: Unable to start activity ComponentInfo{stiw47.logos/stiw47.logos.AndroidRssReader}: android.os.NetworkOnMainThreadException
E/AndroidRuntime(13216):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
E/AndroidRuntime(13216):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
E/AndroidRuntime(13216):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
E/AndroidRuntime(13216):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
E/AndroidRuntime(13216):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(13216):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(13216):    at android.app.ActivityThread.main(ActivityThread.java:5081)
E/AndroidRuntime(13216):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(13216):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(13216):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime(13216):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
E/AndroidRuntime(13216):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(13216): Caused by: android.os.NetworkOnMainThreadException
E/AndroidRuntime(13216):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
E/AndroidRuntime(13216):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
E/AndroidRuntime(13216):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
E/AndroidRuntime(13216):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
E/AndroidRuntime(13216):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
E/AndroidRuntime(13216):    at java.net.URL.openStream(URL.java:470)
E/AndroidRuntime(13216):    at stiw47.logos.AndroidRssReader.onCreate(AndroidRssReader.java:41)
E/AndroidRuntime(13216):    at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(13216):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(13216):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
E/AndroidRuntime(13216):    ... 11 more
W/ActivityManager( 2143):   Force finishing activity stiw47.logos/.AndroidRssReader
W/ActivityManager( 2143):   Force finishing activity stiw47.logos/.MainActivity

注意1:我在Manifest中声明了一个活动并设置了Internet权限 注意2:当我将AndroidRssReader.java类编译为" standalone"应用它的工作,但当我尝试在"我的应用程序"中运行AndroidRssReader.java类时它崩溃了。

我错在哪里?

2 个答案:

答案 0 :(得分:0)

最重要的一行是这一行:

E/AndroidRuntime(13216): java.lang.RuntimeException: Unable to start activity ComponentInfo{stiw47.logos/stiw47.logos.AndroidRssReader}: android.os.NetworkOnMainThreadException

问题不在于启动Activity,问题是此Activity正在主UI线程中执行网络操作。 Android禁止这样做,因为这样做不允许用户与您的应用互动,从而导致ANR(这将使用户体验非常差,他们可能不再使用您的应用)。

只需将网络代码放在AsyncTask中即可。有关详细信息,请查看here

答案 1 :(得分:0)

你犯的错误是你的代码中有某处( 完全在课堂上 AndroidRssReader.java Line 41)。您正在主要线程 UIThread )中执行网络呼叫或http请求。你应该把那些代码放在[AsyncTask][1]里面。

选中此tutorial,了解如何在Android中使用AsyncTask

编辑:

onCreate()方法中检索xml并解析它的代码应该在AsyncTask中,如下所示:

new AsyncTask<Void, Void, String>() {

            @Override
            protected String doInBackground(Void... params) {

                String result = "Cannot connect RSS!";
                try {
                      URL rssUrl = new URL("http://feeds.feedburner.com/Android-er?format=xml");
                      SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
                      SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
                      XMLReader myXMLReader = mySAXParser.getXMLReader();
                      RSSHandler myRSSHandler = new RSSHandler();
                      myXMLReader.setContentHandler(myRSSHandler);
                      InputSource myInputSource = new InputSource(rssUrl.openStream());
                      myXMLReader.parse(myInputSource);

                      result = streamTitle;

                     } catch (MalformedURLException e) {
                      e.printStackTrace();
                     } catch (ParserConfigurationException e) {
                      e.printStackTrace();
                     } catch (SAXException e) {
                      e.printStackTrace();
                     } catch (IOException e) {
                      e.printStackTrace();
                     }
                return result;
            }

            @Override
            protected void onPostExecute(String response) {
                if(response != null) {
                    result.setText(response);
                }
            }
        }.execute();