ArrayAdapter和ListView的NullPointerException错误

时间:2013-10-29 19:01:18

标签: listview nullpointerexception android-arrayadapter

我有一个列表视图,通过键“hubahuba”从不同活动的编辑文本接收用户输入。当我点击包含此.class的水龙头时,我收到基于logcat的NullPointerException错误,我知道它与我的“String trackinput”行有关,请帮助任何人????

 public static ArrayList<String> list = new ArrayList<String>();
 public static ArrayAdapter<String> adapter;

LisstView lv;
String trackinput;

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

           //listViewCode

    ListView lv = (ListView)findViewById(android.R.id.list);

            //Intent to receive from other activity

    Intent i = getIntent();

    String trackinput = i.getExtras().getString("hubahuba");

            //basic adapter

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
    lv.setAdapter(adapter);
    list.add(0, trackinput);
    adapter.notifyDataSetChanged();
    setListAdapter(adapter);

}

这是从活动1转到活动2中的listView:

public void onClick(View v) { 
 Intent i = new Intent(this, ArgueListActivity.class); 
 i.putExtra("hubahuba",whatedit.getText().toString()); 
 whatedit.setText("");
}

1 个答案:

答案 0 :(得分:0)

您不必创建Intent

的新实例

你可以这样做:

String trackinput = getIntent().getStringExtra("hubahuba");

同样Collin指出

LisstView lv;

ListView lv = (ListView)findViewById(android.R.id.list);

含糊不清

<小时/> 将数据放入intent后,您必须启动活动,并将其传递给intent 它。

public void onClick(View v) { 
 Intent i = new Intent(this, ArgueListActivity.class); 
 i.putExtra("hubahuba",whatedit.getText().toString()); 
 whatedit.setText("");
 startActivity(i)
}