简单适配器空指针

时间:2014-10-15 20:24:40

标签: android android-listview nullpointerexception simpleadapter

我正在尝试使用SimpleAdapter将来自另一个活动的EditText中的数据存储到ListView中,该Save Todo使用来自另一个XML文件的预定义行。所有这些都在名为import java.util.ArrayList; import java.util.HashMap; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.SimpleAdapter; import android.widget.Spinner; public class Add_Item extends ActionBarActivity { //variables EditText TaskNameET; Spinner SpinType; Button SaveTodo; String SpinnerOptions[] = {"OptionOne", "OptionTwo", "OptionThree"}; SimpleAdapter ListAdapt; //call mainactivity class MainActivity Main; //declare arraylist and HashMap ArrayList<HashMap<String, String>> mylist; HashMap<String, String> map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add__item); //define vars TaskNameET = (EditText) findViewById(R.id.TaskNameET); SpinType = (Spinner) findViewById(R.id.spinner1); SaveTodo = (Button) findViewById(R.id.button1); //initialize mylist and map mylist = new ArrayList<HashMap<String, String>>(); map = new HashMap<String, String>(); //SimpleAdapter ListAdapt = new SimpleAdapter(this, mylist, R.layout.row, new String[] {"columnone", "columntwo", "columnthree"}, new int[] {R.id.columnone, R.id.columntwo, R.id.columnthree}); //adapter for spinner ArrayAdapter<String> ard=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, SpinnerOptions); SpinType.setAdapter(ard); //onclick todobutton SaveTodo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { map.put("columnone", Add_Item.this.TaskNameET.getText().toString()); map.put("columntwo", Add_Item.this.TaskNameET.getText().toString()); map.put("columnthree", Add_Item.this.TaskNameET.getText().toString()); mylist.add(map); Main.MainList.setAdapter(ListAdapt); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.add__item, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 的按钮中定义。一切正常但是在存储数据时,我得到一个空指针错误。图片http://i.imgur.com/k95cilO.png

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.moduleonesimpleapplication.Add_Item" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:text="@string/TitleText"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/TaskNameET"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="34dp"
        android:ems="10"
        android:inputType="text" >

        <requestFocus />

        <requestFocus />

    </EditText>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/TaskNameET"
        android:layout_below="@+id/TaskNameET"
        android:layout_marginTop="27dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/TaskNameET"
        android:layout_alignParentLeft="true"
        android:text="@string/TaskNameTXT" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="48dp"
        android:text="@string/TaskDateTXT" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/spinner1"
        android:ems="10"
        android:inputType="date" >
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/editText2"
        android:text="@string/SaveButton" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/spinner1"
        android:layout_alignLeft="@+id/textView2"
        android:layout_marginBottom="19dp"
        android:text="@string/TaskTypeTxT" />

</RelativeLayout>

// R.layout Add_Item XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

        <TextView
            android:id="@+id/columnone"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:textColor="#C0C0C0"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/columntwo"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:textColor="#C0C0C0"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/columnthree"
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:textColor="#C0C0C0"
            android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

// R.layout Row.xml

{{1}}

1 个答案:

答案 0 :(得分:1)

您必须在onCreate方法中初始化所有变量(地图,列表等),而不是在“声明”部分。

我希望这会有所帮助;)

编辑:

根据您的评论:setAdapter正在应用于MainActivity的“null实例”。为什么不在MainActivity中创建一个修改适配器的静态方法?您必须以静态方式(nameClass.nameMethod(..))调用它。

确保始终在MainActivity之后创建此活动!

在您的主要活动中:

  • 制作MainList静态
  • 声明一个静态的“mathod,可以访问它:

    public static ListView getMainList(){return MainList;}

Add_activity

  • 删除您的声明MainActivity main;
  • 更改第MainActivty.Main.setAdap...

    MainActivity.getMainList()setAdapter(ListAdapt);

尽管如此......我认为你缺少一些Android基础知识点,因为对我来说这是一个非常难的开发方式。

祝你好运!