不幸的是应用程序停止了

时间:2012-07-16 11:25:21

标签: android android-emulator

  

可能重复:
  detail of list item in another screen

当我选择一个项目时,我希望应用程序导航到第二个活动,并获取所选项目的值以及第二个活动中存在的TextView的相关链接。代码应该工作,但是当我运行它时,模拟器给我一条消息说“不幸的是应用程序停止了”。我不知道该怎么办才能让它发挥作用?

public class LastActivity extends ListActivity {
    /** Called when the activity is first created. */

    static List<String> links;
    List<String> names;


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String link=links.get(position);

        Intent intent = new Intent(getApplicationContext(),Details.class);
        intent.putExtra("name",names.get(position));
        intent.putExtra("url",link);
        Log.e("n",names.get(position)+"."+ link );
        startActivity(intent);


    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        links=new ArrayList<String>();
        names=new ArrayList<String>();
        try{
            URL url=new URL(webservice);
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(getInputStream(url), "UTF_8");
            boolean insideItem = false;


                    // Returns 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("Name")) {

                            if (insideItem)

                                names.add(xpp.nextText()); //extract the headline

                        } else if (xpp.getName().equalsIgnoreCase("url")) {

                            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(); //move to next element

                }


        }catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (XmlPullParserException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);

                setListAdapter(adapter);

 }
    private InputStream getInputStream(URL url) {
        // TODO Auto-generated method stub
        try {
             return url.openConnection().getInputStream();
             } catch (IOException e) {
                   return null;
            }

    }

}

//第二课或爱心人

public class Details extends LastActivity{
    LastActivity last=new LastActivity();
    TextView tv;
    TextView url;
    String read;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);

        tv=(TextView) findViewById(R.id.text);
        url=(TextView) findViewById(R.id.link);
        Intent intent=getIntent();

            // receiving  data
            String name = intent.getStringExtra("name");
            String path=intent.getStringExtra("links");

            Log.e("Second Screen", name + "." + path);
            tv.setText(name);
            url.setText(path);
    }

1 个答案:

答案 0 :(得分:1)

我认为问题在于在意图中传递putExtra值。检查您的第一个活动,您已将密钥用作“网址”,但在详细信息活动中,您使用“链接”作为密钥。所以你得到空指针异常。正确地改变它。

    intent.putExtra("name",names.get(position));
    intent.putExtra("url",link);
<详细活动中的

        String name = intent.getStringExtra("name");
        String path=intent.getStringExtra("url");

代替,

String path=intent.getStringExtra("links");

在您的第一个活动中,您已经扩展了ListActivty,尝试将其替换为单独的Activity。

或者将您的ListView ID更改为xml文件中的@android:list。

有关详细信息,请查看此问题"Stopped Unexpectedly" tried 10000 times to fix

还要检查一下这个想法,

Your content must have a ListView whose id attribute is 'android.R.id.list