向用户显示ArrayList

时间:2019-03-21 02:12:20

标签: android android-intent arraylist

我有两个活动,一个是MainActivity,另一个是Main2Activity。 在Main2Activity中,我将从用户在TextView中输入内容,并将其存储在ArrayList中,现在,我想将此ArrayList发送给MainActivity,在其中将其显示给用户。 我用过意图。 但是我的应用程序崩溃了。 以下是我的两个活动代码 主要活动

    package com.example.to_doapp;

   import android.content.Intent;
   import android.os.Bundle;
   import android.support.design.widget.FloatingActionButton;
   import android.support.design.widget.Snackbar;
   import android.support.v7.app.AppCompatActivity;
   import android.support.v7.widget.Toolbar;
   import android.view.View;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.widget.LinearLayout;
   import android.widget.TextView;
   import android.widget.Toast;
   import java.io.Serializable;
   import java.util.ArrayList;

   public class MainActivity extends AppCompatActivity {

   public void onclick (View view  )
   {

        Intent intent = new Intent(getApplicationContext(), 
            Main2Activity.class );
        startActivity(intent);
  }

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    Intent intent = getIntent();
    //Toast.makeText (this,intent.getStringExtra("task"), Toast.LENGTH_SHORT ).show() ;

    Bundle args = intent.getBundleExtra("BUNDLE");
     ArrayList< String> object  =  (ArrayList<String>) args.getSerializable("ARRAYLIST");
    LinearLayout lView = new LinearLayout(this);
    TextView myText= new TextView(this);
    lView.addView(myText);
    for (int i=0; i<object.size();i++){
        myText.append(object.get(i));
        myText.append("\n");
    }
    setContentView(lView);

 }

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

Main2Activity是         包com.example.to_doapp;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.EditText;
    import java.io.Serializable;
    import java.util.ArrayList;

    public class Main2Activity extends AppCompatActivity {

    ArrayList< String> arr = new ArrayList < String > ();
    public void BackMain ( View view )
    {
        Intent intent = new Intent( getApplicationContext() 
           ,MainActivity.class ) ;
       Bundle args = new Bundle();
        EditText editText = ( EditText) findViewById( R.id.editText3) ;
        String s = editText.getText().toString();
       arr.add ( s ) ;
       args.putSerializable("ARRAYLIST",(Serializable)arr);
        intent.putExtra("bundle",args);
       startActivity(intent);
     }

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
         fab.setOnClickListener(new View.OnClickListener() {
         @Override
        public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", 
            Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
       });
       }

       }

我的Logcat显示这样的内容

  

03-21 07:55:00.407 763-826 /? E / ConnectivityService:RemoteException   尝试发送NetworkRequest [id = 81,的回调消息,   legacyType = -1,[功能:INTERNET&NOT_RESTRICTED&TRUSTED]]   03-21 07:55:58.292 29529-29529 /? E / MPlugin:不支持的类:   com.mediatek.common.telephony.IOnlyOwnerSimSupport 03-21 07:56:01.380   29645-29672 /? E / MPlugin:不支持的类:   com.mediatek.common.telephony.IOnlyOwnerSimSupport 03-21 07:56:01.577   29624-29624 / com.example.to_doapp E / AndroidRuntime:致命异常:   主要       流程:com.example.to_doapp,PID:29624       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.to_doapp / com.example.to_doapp.MainActivity}:   java.lang.NullPointerException:尝试调用虚拟方法   'java.io.Serializable   android.os.Bundle.getSerializable(java.lang.String)'在空对象上   参考           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)           在android.app.ActivityThread.access $ 800(ActivityThread.java:178)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1470)           在android.os.Handler.dispatchMessage(Handler.java:111)           在android.os.Looper.loop(Looper.java:194)           在android.app.ActivityThread.main(ActivityThread.java:5643)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:960)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)        原因:java.lang.NullPointerException:尝试调用虚拟方法'java.io.Serializable   android.os.Bundle.getSerializable(java.lang.String)'在空对象上   参考           在com.example.to_doapp.MainActivity.onCreate(MainActivity.java:51)           在android.app.Activity.performCreate(Activity.java:6100)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)           在android.app.ActivityThread.access $ 800(ActivityThread.java:178)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1470)           在android.os.Handler.dispatchMessage(Handler.java:111)           在android.os.Looper.loop(Looper.java:194)           在android.app.ActivityThread.main(ActivityThread.java:5643)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:960)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)03-21   07:56:01.787 29645-29670 /? E / WifiManager:mWifiServiceMessenger ==   空03-21 07:56:03.275 29764-29764 /? E / MPlugin:不支持的类:   com.mediatek.common.telephony.IOnlyOwnerSimSupport 03-21 07:56:03.390   29645-29671 /? E / NativeCrypto:ssl = 0xb47ac800 cert_verify_callback   x509_store_ctx = 0x9fa343e0 arg = 0x0 03-21 07:56:03.390 29645-29671 /?   E / NativeCrypto:ssl = 0xb47ac800 cert_verify_callback调用   verifyCertificateChain authMethod = ECDHE_ECDSA 03-21 07:56:03.536   29645-29672 /? E / NativeCrypto:ssl = 0xaf83da00 cert_verify_callback   x509_store_ctx = 0x9f930460 arg = 0x0 03-21 07:56:03.536 29645-29672 /?   E / NativeCrypto:ssl = 0xaf83da00 cert_verify_callback调用   verifyCertificateChain authMethod = ECDHE_ECDSA 03-21 07:56:03.584   29787-29787 /? E / WifiManager:mWifiServiceMessenger ==空03-21   07:56:04.382 29787-29787 /? E / MPlugin:不支持的类:   com.mediatek.common.telephony.IOnlyOwnerSimSupport 03-21 07:56:04.743   29838-29838 /? E / MPlugin:不支持的类:   com.mediatek.common.telephony.IOnlyOwnerSimSupport 03-21 07:56:11.598   29645-29681 /? E / Finsky:[4373] fng.a(20):无法建立选择器:   /storage/sdcard0/Download/marketenvs.csv:打开失败:ENOENT(否   文件或目录)03-21 07:56:11.772 29645-29724 /? E / SQLiteLog:(283)   从WAL文件中恢复了63帧   /data/data/com.android.vending/databases/phenotype.db-wal 03-21   07:56:55.470 763-763 /? E / WifiTrafficPoller:ENABLE_TRAFFIC_STATS_POLL   false令牌226 03-21 07:56:55.535 763-823 /? E / WifiStateMachine:   ScanModeState(当= -58ms时= 131167!CMD_SCREEN_STATE_CHANGED {   when = -58ms what = 131167   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.536 763-823 /? E / WifiStateMachine:DriverStartedState   (当= -58ms时= 131167!CMD_SCREEN_STATE_CHANGED {何时= -58ms   what = 131167 target = com.android.internal.util.StateMachine $ SmHandler}   0 0 03-21 07:56:55.536 763-823 /? E / WifiStateMachine:   SupplicantStartedState(当= -59ms时   什么= 131167!CMD_SCREEN_STATE_CHANGED {何时= -59ms什么= 131167   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.537 763-823 /? E / WifiStateMachine:DefaultState(当= -60ms时   what = 131167!CMD_SCREEN_STATE_CHANGED {when = -60ms what = 131167   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.547 763-823 /? E / WifiStateMachine:cancelDelayedScan-> 228   03-21 07:56:55.547 763-823 /? E / WifiStateMachine:   handleScreenStateChanged退出:否03-21 07:56:55.548 763-823 /?   E / WifiStateMachine:ScanModeState(当= -4ms时   什么= 131154CMD_ENABLE_RSSI_POLL {何时= -4ms什么= 131154   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.550 763-823 /? E / WifiStateMachine:DriverStartedState   (当= -6ms时= 131154CMD_ENABLE_RSSI_POLL {当= -6ms时= 131154   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.550 763-823 /? E / WifiStateMachine:SupplicantStartedState   (当= -6ms时= 131154CMD_ENABLE_RSSI_POLL {当= -6ms时= 131154   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.551 763-823 /? E / WifiStateMachine:DefaultState(当= -6ms时   什么= 131154CMD_ENABLE_RSSI_POLL {何时= -7ms什么= 131154   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.551 763-823 /? E / WifiStateMachine:ScanModeState(当= -6ms时   什么= 131158 arg1 = 1CMD_SET_SUSPEND_OPT_ENABLED {当= -6ms什么= 131158   arg1 = 1 target = com.android.internal.util.StateMachine $ SmHandler} 1 0   03-21 07:56:55.552 763-823 /? E / WifiStateMachine:DriverStartedState   (当= -7ms时= 131158 arg1 = 1CMD_SET_SUSPEND_OPT_ENABLED {当= -7ms时   什么= 131158 arg1 = 1   target = com.android.internal.util.StateMachine $ SmHandler} 1 0 03-21   07:56:55.574 763-823 /? E / WifiStateMachine:   setSuspendOptimizationsNative:4是-想要是   stack:setSuspendOptimizationsNative-访问$ 16400-processMessage-   processMsg 03-21 07:56:55.575 763-823 /? E / WifiStateMachine:   setSuspendOptimizationsNative做到4 true   stack:setSuspendOptimizationsNative-访问$ 16400-processMessage-   processMsg 03-21 07:56:55.585 763-823 /? E / native:确实暂停   03-21 07:56:55.599 763-823 /? E / WifiStateMachine:ScanModeState   (当= -53ms时= 131248M_CMD_NOTIFY_SCREEN_OFF {当= -53ms时= 131248   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.599 763-823 /? E / WifiStateMachine:DriverStartedState   (当= -54ms时= 131248M_CMD_NOTIFY_SCREEN_OFF {当= -54ms时= 131248   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.600 763-823 /? E / WifiStateMachine:SupplicantStartedState   (当= -54ms时= 131248M_CMD_NOTIFY_SCREEN_OFF {当= -55ms时= 131248   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:55.601 763-823 /? E / WifiStateMachine:DefaultState(当= -55ms时   什么= 131248M_CMD_NOTIFY_SCREEN_OFF {当= -55ms什么= 131248   target = com.android.internal.util.StateMachine $ SmHandler} 0 0 03-21   07:56:56.504 1773-29908 /? E / MPlugin:不支持的类:   com.mediatek.common.telephony.IOnlyOwnerSimSupport 03-21 07:56:56.635   763-779 /? E /传感器:handleToDriver手柄(0)03-21 07:56:56.636   763-779 /? E /传感器:handleToDriver手柄(0)03-21 07:56:56.638   763-779 /? E /传感器:新的setDelay句柄(0),ns(20000000)m,错误(0),   index(2)03-21 07:57:00.388 763-1406 /? E /传感器:handleToDriver   手柄(0)03-21 07:57:28.639 1673-13747 /? E /唤醒锁:GCM_HB_ALARM   释放而没有匹配的获取!

2 个答案:

答案 0 :(得分:0)

要将arrayList对象传递给另一个活动,请使用此代码。

在Main2Activity中

ArrayList<String> arr = new ArrayList<String>();
EditText editText = ( EditText) findViewById( R.id.editText3) ;
String s = editText.getText().toString();
arr.add (s) ;
intent.putExtra("mylist", arr);

在MainActivity中

ArrayList<String> object = (ArrayList<String>)getIntent().getSerializableExtra("mylist");

答案 1 :(得分:0)

您的应用程序崩溃了,因为从日志中查看:

  

java.lang.NullPointerException:尝试在com.example.to_doapp.MainActivity.onCreate上的空对象引用上调用虚拟方法'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' ( MainActivity.java:51

在Main2Activity的方法public void BackMain ( View view )(建议根据Java naming conventions将方法重命名为“ backMain”)中,您有intent.putExtra("bundle",args);,其中多余数据的键(或名称)第一个参数是“ bundle”。但是,从MainActivity获取此额外数据时,您将拥有Bundle args = intent.getBundleExtra("BUNDLE");,其中捆绑包的名称为“ BUNDLE”,该名称与“ bundle”不匹配,因此它将返回null。您需要为要通过Intent从一个Activity传递到另一个Activity的额外数据使用相同的名称(在本例中为Bundle)。