简体   繁体   English

从另一个活动中启动带有ListView的活动的麻烦

[英]trouble with launching an activity with ListView from another activity

**I have a strange problem (for me) when i launch an activity and this activity has a listview from another activity it didn't work but when i send a string to fill the listview it launches !! **我有一个奇怪的问题(对我而言),当我启动一个活动,并且该活动具有另一个活动的列表视图时,它不起作用,但是当我发送字符串以填充列表视图时,它启动! here is the activity from where i will launch the another activity (the problem is in the second intent which will go to show.class ) 这是我将要启动另一个活动的活动(问题出在第二个意图上,它将显示到show.class)

package com.hema.colornotes;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {

    Button b1,b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.btAdd);
        b2 = (Button) findViewById(R.id.btShow);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i =new Intent(MainActivity.this,add.class);
                startActivity(i);
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, show.class);
                startActivity(i);
            }
        });
    }




}

and here is the activity i need to launch 这是我需要启动的活动

package com.hema.colornotes;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class show extends ActionBarActivity {
    Context context;
    List<String> tasks;
    ArrayAdapter<String> adapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        context = this;
        tasks = new ArrayList<String>();


        // second parameter is row layout,
        adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,tasks);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        Intent intent = getIntent();
        String value = intent.getExtras().getString("message");

        tasks.add(value);

        // this method will refresh your listview manually
        adapter.notifyDataSetChanged();


    }


}

and that's the activity which worked well and launched the show activity because i sent a string 这是一项效果很好的活动,并启动了表演活动,因为我发送了一个字符串

package com.hema.colornotes;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class add extends ActionBarActivity {
    EditText note;

    Button b1,b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        b1=(Button)findViewById(R.id.btSave);
        b2=(Button)findViewById(R.id.btCancel);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                note = (EditText) findViewById(R.id.etNote);
                String notes = note.getText().toString();
                Intent i = new Intent(add.this, show.class);
                i.putExtra("message", notes);
                startActivity(i);
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(add.this, MainActivity.class);
                startActivity(i);

            }
        });


    }


}

thanks in advance !** 提前致谢 !**

When you dont send bundle to show activity, It would have crashed the app with null pointer exception. 当您不发送捆绑包以显示活动时,它将以空指针异常使应用程序崩溃。 That is why, it did not work. 这就是为什么,它没有用。 When you send the bundle, show activity gets the required data to display. 发送捆绑包时,show activity将显示所需的数据。 So it works fine! 所以工作正常!

您的第二个活动被设置为从意图中获取“消息”,因此当您不输入任何字符串时,您的第二个活动将崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM