引发者:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'int java.util.List.size()'

时间:2016-10-24 14:28:06

标签: android nullpointerexception

我有一个错误,“java.lang.NullPointerException:尝试在空对象引用上调用接口方法'int java.util.List.size()'。”我想也许我忘了为某事物指定值,但我不知道究竟是什么。我的arrayadapter或arraylist有什么问题吗?

package com.example.lyn.finalproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;

import java.util.ArrayList;

/**
 * Created by lyn on 2016/10/22.
 */
public class SecondActivity extends Activity {
    Button bn1;
    Button bn2;
    Button bn3;
    Button bn4;
    Button bn5;
    Button bn6;
    EditText etext1;
    EditText etext2;
    EditText etext3;
    EditText etext4;
    EditText etext5;
    EditText etext6;
    String cname1;
    String cname2;
    String cname3;
    String cname4;
    String cname5;
    String cname6;
    String ctime1;
    String ctime2;
    String ctime3;
    String ctime4;
    String ctime5;
    String ctime6;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        etext1=(EditText) findViewById(R.id.adcourse1);
        etext2=(EditText) findViewById(R.id.adcourse2);
        etext3=(EditText) findViewById(R.id.adcourse3);
        etext4=(EditText) findViewById(R.id.adcourse4);
        etext5=(EditText) findViewById(R.id.adcourse5);
        etext6=(EditText) findViewById(R.id.adcourse6);
        bn1=(Button) findViewById(R.id.menub1);
        bn2=(Button) findViewById(R.id.menub2);
        bn3=(Button) findViewById(R.id.menub3);
        bn4=(Button) findViewById(R.id.menub4);
        bn5=(Button) findViewById(R.id.menub5);
        bn6=(Button) findViewById(R.id.menub6);
        //为每一个按钮绑定popupMenu,并且接收用户点击的课程时间信息
        bn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu menu1=new PopupMenu(SecondActivity.this,bn1);
                           menu1.getMenuInflater().inflate(R.menu.time_choose_menu,
menu1.getMenu());
            menu1.setOnMenuItemClickListener(new     PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        ctime1=menuItem.getTitle()+"";
                        Log.d("TAG",ctime1);
                        return true;
                    }
                });
                menu1.show();
            }
        });
        bn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu menu2=new PopupMenu(SecondActivity.this,bn2);
                menu2.getMenuInflater().inflate(R.menu.time_choose_menu,menu2.getMenu());
                menu2.setOnMenuItemClickListener(new     PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        ctime2=menuItem.getTitle()+"";
                        Log.d("TAG",ctime2);
                        return true;
                    }
                });
        menu2.show();
            }
        });
   bn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu menu3=new PopupMenu(SecondActivity.this,bn3);
            menu3.getMenuInflater().inflate(R.menu.time_choose_menu,menu3.getMenu());
            menu3.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    ctime3=menuItem.getTitle()+"";
                    Log.d("TAG",ctime3);
                    return true;
                }
            });
            menu3.show();
        }
    });
    bn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu menu4=new PopupMenu(SecondActivity.this,bn4);
            menu4.getMenuInflater().inflate(R.menu.time_choose_menu,menu4.getMenu());
            menu4.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    ctime4=menuItem.getTitle()+"";
                    Log.d("TAG",ctime4);
                    return true;
                }
            });
            menu4.show();
        }
    });
    bn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu menu5=new PopupMenu(SecondActivity.this,bn5);
            menu5.getMenuInflater().inflate(R.menu.time_choose_menu,menu5.getMenu());
            menu5.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    ctime5=menuItem.getTitle()+"";
                    Log.d("TAG",ctime5);
                    return true;
                }
            });
            menu5.show();
        }
    });
    bn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu menu6=new PopupMenu(SecondActivity.this,bn6);
            menu6.getMenuInflater().inflate(R.menu.time_choose_menu,menu6.getMenu());
            menu6.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    ctime6=menuItem.getTitle()+"";
                    Log.d("TAG",ctime6);
                    return true;
                }
            });
            menu6.show();
        }
    });
    //接受用户输入的课程名称
    cname1=etext1.getText()+"";
    cname2=etext2.getText()+"";
    cname3=etext3.getText()+"";
    cname4=etext4.getText()+"";
    cname5=etext5.getText()+"";
    cname6=etext6.getText()+"";
    Button bn=(Button) findViewById(R.id.bn);
    bn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ArrayList<String> courseName=new ArrayList<String>();
            courseName.add(0,cname1);
            courseName.add(1,cname2);
            courseName.add(2,cname3);
            courseName.add(3,cname4);
            courseName.add(4,cname5);
            courseName.add(5,cname6);
            ArrayList<String> courseTime=new ArrayList<String>();
            courseTime.add(0,ctime1);
            courseTime.add(1,ctime2);
            courseTime.add(2,ctime3);
            courseTime.add(3,ctime4);
            courseTime.add(4,ctime5);
            courseTime.add(5,ctime6);
            Intent intent=new  Intent(SecondActivity.this,ThirdActivity.class);
            intent.putStringArrayListExtra("list", courseName);
            intent.putStringArrayListExtra("list",courseTime);
            startActivity(intent);
        }
    });
}

}


package com.example.lyn.finalproject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyn on 2016/10/23.
 */
public class ThirdActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);
    Intent intent1=getIntent();
    Intent intent2=getIntent();
    Log.d("TAG","HI");
    ArrayList<String> courseName=intent1.getStringArrayListExtra("courseName");
    ArrayList<String> courseTime=intent2.getStringArrayListExtra("courseTime");
    ArrayAdapter<String> adapter1=new ArrayAdapter<>(ThirdActivity.this,android.R.layout.simple_list_item_1,courseName);
    ArrayAdapter<String> adapter2=new ArrayAdapter<>(ThirdActivity.this,android.R.layout.simple_list_item_1,courseTime);
    ListView listView1=(ListView) findViewById(R.id.list_view1);
    ListView listView2=(ListView) findViewById(R.id.list_view2);
    listView1.setAdapter(adapter1);
    listView2.setAdapter(adapter2);
    }
}

activity_third.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
    android:id="@+id/list_view1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
</ListView>
<ListView
    android:id="@+id/list_view2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
</ListView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您列出的代码中有两个问题,即您的代码为何会抛出NullPointerException以及为什么它不起作用。

首先,“putStringArrayListExtra(String name,ArrayList value)”和“getStringArrayExtra(String name)”中的名称必须相同

  

intent.putStringArrayListExtra(“ list ”,courseName); //更改为“courseName”   intent.putStringArrayListExtra( “<强>列表”,courseTime); //改为“courseTime”

不匹配
  

ArrayList courseName = intent1.getStringArrayListExtra(“ courseName ”);

  ArrayList courseTime = intent2.getStringArrayListExtra(“ courseTime ”);

其次,您需要检索调用意图,而不是创建新意图。

  

Intent intent = getIntent();

     

ArrayList courseName = intent.getStringArrayListExtra(“ courseName ”);

  ArrayList courseTime = intent.getStringArrayListExtra(“ courseTime ”);