有人可以帮我检查我的代码有什么问题吗?我有4个,spinner
并且它们的值已插入到中SQLite
。现在我想将该项目检索到另一个旋转器中,Update.java
但得到java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
我已经检查了每个步骤,但仍然无法找出问题所在。
更新.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new MyDatabaseHelper(this);
setContentView(R.layout.updatepage);
final String name = getIntent().getExtras().getString("name1");
final String date=getIntent().getExtras().getString("date1");
final String ID = getIntent().getExtras().getString("ID");
RetrievePage(name,date,ID);
}
public void addProject1(String c)
{
project1=(Spinner)findViewById(R.id.spinner8);
String[] arr = new String[]{"Pro-XXX-XXX","TRN-XXX-XXX","SRV-XXX-XXX","PRO-REE-BERGARDING","PRO-SKM-SSS","PRO-SKM-DI","PRO-SKM-PACKING","PRO-SIE-AVAGO_S110",
"PRO-SIE-BIOCON","PRO-BCM-T13/T17","TRIN-IN_HOUSE","TRIN-EXTERNAL","SRIV-SIE-LEMONGRASS","SRV-PW-SIMOCODE","SRV-REE-SIMOCODE","PRO-SKM-D5","PRO-SKM-DRIOU","PRO-BKI-DESMET",
"PRO-BKI-INFINEON","PRO-INH-ANDROID","PRO-BCM-S120","PRO-GAW-OP"};
List<String> list = new ArrayList<String>();
String project11 = c;
list.add(project11);
for(String s:arr){
if(!list.contains(s)){
list.add(s);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project1.setAdapter(adapter);
}
public void addProject2(String d)
{
project2=(Spinner)findViewById(R.id.spinner9);
String[] arr = new String[]{"Pro-XXX-XXX","TRN-XXX-XXX","SRV-XXX-XXX","PRO-REE-BERGARDING","PRO-SKM-SSS","PRO-SKM-DI","PRO-SKM-PACKING","PRO-SIE-AVAGO_S110",
"PRO-SIE-BIOCON","PRO-BCM-T13/T17","TRIN-IN_HOUSE","TRIN-EXTERNAL","SRIV-SIE-LEMONGRASS","SRV-PW-SIMOCODE","SRV-REE-SIMOCODE","PRO-SKM-D5","PRO-SKM-DRIOU","PRO-BKI-DESMET",
"PRO-BKI-INFINEON","PRO-INH-ANDROID","PRO-BCM-S120","PRO-GAW-OP"};
List<String> list = new ArrayList<String>();
String project22 = d;
list.add(project22);
for(String s:arr){
if(!list.contains(s)){
list.add(s);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project2.setAdapter(adapter);
}
public void addProject3(String e)
{
project3=(Spinner)findViewById(R.id.spinner13);
String[] arr = new String[]{"Pro-XXX-XXX","TRN-XXX-XXX","SRV-XXX-XXX","PRO-REE-BERGARDING","PRO-SKM-SSS","PRO-SKM-DI","PRO-SKM-PACKING","PRO-SIE-AVAGO_S110",
"PRO-SIE-BIOCON","PRO-BCM-T13/T17","TRIN-IN_HOUSE","TRIN-EXTERNAL","SRIV-SIE-LEMONGRASS","SRV-PW-SIMOCODE","SRV-REE-SIMOCODE","PRO-SKM-D5","PRO-SKM-DRIOU","PRO-BKI-DESMET",
"PRO-BKI-INFINEON","PRO-INH-ANDROID","PRO-BCM-S120","PRO-GAW-OP"};
List<String> list = new ArrayList<String>();
String project33 = e;
list.add(project33);
for(String s:arr){
if(!list.contains(s)){
list.add(s);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project3.setAdapter(adapter);
}
public void addProject4(String f)
{
project4=(Spinner)findViewById(R.id.spinner14);
String[] arr = new String[]{"Pro-XXX-XXX","TRN-XXX-XXX","SRV-XXX-XXX","PRO-REE-BERGARDING","PRO-SKM-SSS","PRO-SKM-DI","PRO-SKM-PACKING","PRO-SIE-AVAGO_S110",
"PRO-SIE-BIOCON","PRO-BCM-T13/T17","TRIN-IN_HOUSE","TRIN-EXTERNAL","SRIV-SIE-LEMONGRASS","SRV-PW-SIMOCODE","SRV-REE-SIMOCODE","PRO-SKM-D5","PRO-SKM-DRIOU","PRO-BKI-DESMET",
"PRO-BKI-INFINEON","PRO-INH-ANDROID","PRO-BCM-S120","PRO-GAW-OP"};
List<String> list = new ArrayList<String>();
String project44 = f;
list.add(project44);
for(String s:arr){
if(!list.contains(s)){
list.add(s);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project4.setAdapter(adapter);
}
public void RetrievePage(String name,String date, String id) {
final String name2 = name;
final String date2=date;
final String id2 = id;
final EditText name3 = (EditText) findViewById(R.id.editText9);
final EditText date3 = (EditText) findViewById(R.id.editText12);
final EditText per1=(EditText) findViewById(R.id.editText20);
final EditText per2=(EditText) findViewById(R.id.editText24);
final EditText per3=(EditText) findViewById(R.id.editText28);
final EditText per4=(EditText) findViewById(R.id.editText32);
database = dbHelper.getWritableDatabase();
c = database.rawQuery("SELECT i.Weather, i.Status,w.Subcontractors, w.NumberOfPerson, w.NumberOfHours, wd.Project, wd.WorkDescription, wd.Per, wd.TimeIn, wd.TimeOut FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i._id LEFT JOIN WorkDetails wd ON wd.Twf_id=w._id WHERE i.Name = ? AND i._id= ? ",
new String[]{String.valueOf(name2),String.valueOf(id2)}, null);
if (c != null) {
while (c.moveToNext()) {
Details WD = new Details();
String
Weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather));
String Project11=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
String Project22=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
String
Project33=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
String Project44=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
String Per1=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Per));
String Per2=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Per));
String Per3=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Per));
String Per4=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Per));
addProject1(Project11);
addProject2(Project22);
addProject3(Project33);
addProject4(Project44);
addWeather(Weather);
WD.setProject(Project11);
WD.setProject(Project22);
WD.setProject(Project33);
WD.setProject(Project44);
WD.setPer(Per1);
WD.setPer(Per2);
WD.setPer(Per3);
WD.setPer(Per4);
per1.setText(Per1);
per2.setText(Per2);
per3.setText(Per3);
per4.setText(Per4);
}
}
c.close();
}
错误的LogCat
Process: com.example.project.project, PID: 2364
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:194)
at android.widget.Spinner.onMeasure(Spinner.java:580)
at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:410)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChild(ViewGroup.java:5922)
at android.view.ViewGroup.measureChildren(ViewGroup.java:5899)
at android.widget.AbsoluteLayout.onMeasure(AbsoluteLayout.java:66)
我有一个活动,其中有一个微调器,下面的代码对我来说很好用。
public void addWeather(String a) {
weather3 = (Spinner) findViewById(R.id.spinner5);
String[] arr = new String[]{"Sunny","Cloudy","Rainy","Thunderstorm"};
List<String> list = new ArrayList<String>();
String weather = a;
list.add(weather);
for(String s:arr){
if(!list.contains(s)){
list.add(s);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weather3.setAdapter(adapter);
}
addProject
*方法之一的参数是null
。正如您在摘录中看到的ArrayAdapter.createViewFromResource
:
T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
…的check instanceof Charsequence
,如果T不是a CharSequence
或T
null ,则返回false
。由于您的适配器类型,String
我们可以排除第一种情况。因此,集合中的String之一为null。
问题内容: 我正在尝试将播放器的名称保存为共享的首选项,并通过再次以共享的首选项获取它的名称来使其显示在另一个活动中,但是我的应用程序崩溃了。 致命异常:主要 代码: 播放器 PlayGame.java PlayerName.java 问题答案: 您的应用在以下位置崩溃: 因为。 您忘记在PlayGame活动中进行初始化。
问题内容: 运行我的项目时出现错误。 这是完整的日志: 这是我的ProductDetail.java 谁能告诉我错误在哪里以及如何解决?如果有人可以帮助我,我将非常感激。 问题答案: 您的字符串是这样给你一个。这是导致它的原因: 字符串是因为您没有从接收任何值。这可能是因为您忘记了传递以前的值。 但是,您可以检查一下String 是否存在,那么将不会执行这些操作。您可以通过以下方式做到这一点: 因
问题内容: 我正在尝试在RecyclerView中实施滑动删除操作。除了在要刷卡的项目下方绘制一个删除图标外,其他一切似乎都工作正常。 这就是我的做法: 它在扔 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法’boolean android.graphics.Bitmap.isRecycled()’ 全栈跟踪 第149行已调试,但我看到它为null,但
问题内容: 我的Android项目中总是出现错误。 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.app.Activity.findViewById(int)” 在我的MainActivity中,我正在调用FragmentStart类。 FragmentStart.class 问题答案: 除了@S
问题内容: 我有一个称为 MainActivity.java 的类,该类称为一个类。最后一个类在执行时返回此错误: 我不明白在完成工作后如何编辑排名。 MainActivity.java 连接.java 问题答案: 错误是 永远不会初始化,因此指向null。为了使您的代码正常工作,最小的步骤是在MainActivity中 在连接 但是无论如何,在onCreate中创建任务并传递Activity并不
问题内容: 我收到以下错误 尝试在空对象引用上调用虚拟方法’void android.widget.StackView.setAdapter(android.widget.Adapter)’ 在这条线上 完整的片段 EventsFragment.java 是 Stack_Adapter.java Stack_Items 问题答案: 您正在执行: 你是。返回。 你为什么用? 在哪里?您应该从正确的x