单击listview中的项目时出错

时间:2014-02-28 18:48:57

标签: android listview

当我点击主活动中的列表视图的项目以打开另一个活动时出现此错误

Caused by: java.lang.NullPointerException
at com.androidbegin.filterlistviewtutorial.SingleItemView.onCreate(SingleItemView.java:42)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)

MainActivity.java

public class MainActivity extends Activity {

// Declare Variables
ListView list;
ListViewAdapter adapter;
EditText editsearch;
String[] rank;
String[] country;
String[] population;
ArrayList<WorldPopulation> arraylist = new ArrayList<WorldPopulation>();

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

    // Generate sample data
    rank = new String[] { "1", "2", "3" };

    country = new String[] { "Sin El Fil - سن الفيل",
            "Jesr El Basha - جسر الباشا", "Jdeideh - الجديدة" };

    population = new String[] { "009611511989", "009611511989",
            "009611884326"};

    // Locate the ListView in listview_main.xml
    list = (ListView) findViewById(R.id.listview);

    for (int i = 0; i < rank.length; i++) {
        WorldPopulation wp = new WorldPopulation(rank[i], country[i],
                population[i]);
        // Binds all strings into an array
        arraylist.add(wp);
    }

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, arraylist);

    // Binds the Adapter to the ListView
    list.setAdapter(adapter);

    // Locate the EditText in listview_main.xml
    editsearch = (EditText) findViewById(R.id.search);

    // Capture Text in EditText
    editsearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = editsearch.getText().toString()
                    .toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
        }
    });
}

// Not using options menu in this tutorial
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

SingleItemView.java

public class SingleItemView extends Activity {
// Declare Variables
TextView txtrank;
TextView txtcountry;
TextView txtpopulation;
String rank;
String country;
String population;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleitemview);
    // Retrieve data from MainActivity on item click event
    Intent i = getIntent();
    // Get the results of rank
    rank = i.getStringExtra("rank");
    // Get the results of country
    country = i.getStringExtra("country");
    // Get the results of population
    population = i.getStringExtra("population");

    // Locate the TextViews in singleitemview.xml
    txtrank = (TextView) findViewById(R.id.rank);
    txtcountry = (TextView) findViewById(R.id.country);
    txtpopulation = (TextView) findViewById(R.id.population);

    // Load the results into the TextViews
    txtrank.setText(rank);
    txtcountry.setText(country);
    txtpopulation.setText(population);
    /** Defining an onclick listener */
    OnClickListener listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            /** Get Telephone number Object*/
            TextView telNo = (TextView) findViewById(R.id.population);

            /** Get Telephone number String **/
            String strTelNo = telNo.getText().toString();

            /** Creating an intent which invokes an activity whose action name is ACTION_CALL */
            Intent intent = new Intent("android.intent.action.CALL");

            /** Creating a uri object to store the telephone number */
            Uri data = Uri.parse("tel:"+ strTelNo );

            /** Setting intent data */
            intent.setData(data);

            /** Starting the caller activity by the implicit intent */
            startActivity(intent);

        }
    };

    /** Getting reference to button of main.xml */
    Button btn = (Button) findViewById(R.id.button1);        

    /** Setting the event listener for the button */
    btn.setOnClickListener(listener);


}
}

SingleItemView中的第42行:

        txtpopulation.setText(population);

有人会帮我解决这个错误

0 个答案:

没有答案