ArrayList不会存储从另一个intent传递的多个字符串值

时间:2015-05-05 19:09:59

标签: java android android-intent arraylist

我的问题是我有一个意图是将字符串值发送到我的MainActivity。我试图在ArrayList中保存这些值,但只显示最后一个。

这是我的MainActivity:

修改

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){
        Log.d("EmptyExtras", "EmptyExtras");
        return ;
    }
    String product=itemData.getString("product");
    adapter.add(product);

}
@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);
}

}

我添加了adapter.add(“product”)后发生了这样的错误:

 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.add(java.lang.Object)' 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.add(java.lang.Object)' on a null object reference
            at com.example.olev.shoppinglist.MainActivity.addToList(MainActivity.java:45)
            at com.example.olev.shoppinglist.MainActivity.onCreate(MainActivity.java:31)
            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)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

如何在此if子句之后使用Nullpointer以及如何修复它以便将动态项添加到ListView。

2 个答案:

答案 0 :(得分:0)

您的代码正在恢复onPostDoStuff() 两次,先是productNames,另一次是onCreate()。第一次还原后,将新产品String附加到还原列表。但是你再次恢复它,用原始列表替换更新后的列表。

尝试删除onRestoreInstanceState()

答案 1 :(得分:0)

您正在从savedInstanceState而不是getIntent()。getExtras()获取ArrayList,并且您说您想要以意图发送它。 savedInstanceState用于重新创建活动,而intent用于打开新活动。如果您希望将数据从活动发送到另一个活动,那么您应该:

productNames = getIntent().getExtras().getStringArray("products");

savedInstanceState将帮助您在屏幕旋转中保留字符串数组,因为在这种情况下会重新创建活动,但是当活动首次创建时,savedInstanceState为null,因此它甚至不会进入“if”。 / p>