adapter.add()崩溃了应用程序

时间:2015-05-07 13:20:48

标签: android listview android-arrayadapter listitem

我的adapter.add()方法有问题。它导致我的应用程序崩溃。

我的主要活动在这里:

 package com.example.olev.shoppinglist;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;



public class MainActivity extends ActionBarActivity {

ArrayList<String>productNames=new ArrayList<String>();
ArrayAdapter<String> adapter;


public void addNewProduct(View view){
    Intent i = new Intent(this,AddProduct.class);
    startActivity(i);
}

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

    addToList();
    ListView productList=(ListView)findViewById(R.id.productList);
    adapter = new CustomAdapter(this,productNames);

   productList.setAdapter(adapter);
}

public void addToList(){
    Bundle itemData=getIntent().getExtras();

    if(itemData==null){
        return ;
    }
    String product=itemData.getString("product");
    productNames.add(product);
    //adapter.notifyDataSetChanged();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}




}

也是我的AddProduct:

package com.example.olev.shoppinglist;

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.EditText;




public class AddProduct extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_add_product);
}

public void sendProductToMain(View view){
    EditText productName= (EditText)findViewById(R.id.productName);
    String product= productName.getText().toString();
    Intent i = new Intent (this,MainActivity.class);
    i.putExtra("product",product);
    startActivity(i);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

除了使用扩展ArrayAdapter的CustomAdapter。

我的问题是,如果我像这样运行这个代码,我可以将项目添加到listview,但是当我尝试添加另一个时,第一个被覆盖。所以我一直只有一个项目。如果我删除对行adapter.notifyDataSetChanged()的注释;应用程序在到达此行时崩溃。

我也尝试过adapter.add(product);结果与上面的行相同 - 应用程序崩溃。

我在这里搜索了答案,但找不到答案。

我的问题是如何修复它以便我可以动态地将项目添加到ListView ??

修改

错误信息即将到来

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.olev.shoppinglist/com.example.olev.shoppinglist.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference
            at com.example.olev.shoppinglist.MainActivity.addToList(MainActivity.java:48)
            at com.example.olev.shoppinglist.MainActivity.onCreate(MainActivity.java:32)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

1 个答案:

答案 0 :(得分:1)

这是因为您正在从第一个活动向另一个活动发送String代替List。 即假设行putExtra(“product”,“Item1”)用于在第二个活动中发送字符串项item1。当您调用第二个活动时,列表将再次初始化,并且只有一个项目,即“Item1”被添加到列表中。

要发送列表,可以使用intent.putParcelableArrayListExtra(“key”,value);方法