我只是编程的初学者。我的代码有问题。每当我尝试单击列表中的第二项时,应用程序突然崩溃。
这是我的代码:
public class MainActivity extends AppCompatActivity {
ListView lvCategories;
String[] categories = {
"School", "Work", "Family Outings", "Friends Outings", "Groceries" , "Appointments", "Indoor Activities",
"Outdoor Activities", "Games","Overseas Travelling"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvCategories = (ListView)findViewById(R.id.listViewCategories);
final ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, categories);
lvCategories.setAdapter(adapter);
lvCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String category = (String) parent.getItemAtPosition(position);
if (category == "School") {
Intent school = new Intent(view.getContext(), school_activity.class);
school.putExtra("school_category", "");
startActivity(school);
}
if (category == "Work") {
Intent work = new Intent(view.getContext(), work_activity.class);
work.putExtra("work_category", "");
startActivity(work);
}
}
});
}
}
我可以知道这是什么问题吗?
这是我在LogCat中发现的错误。
引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence)'
这是我的work_activity
public class work_activity extends AppCompatActivity {
Button btnSave;
EditText etContent;
TextView tvLastUpdated;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_activity);
btnSave = (Button)findViewById(R.id.buttonSave);
etContent = (EditText)findViewById(R.id.editTextContent);
tvLastUpdated = (TextView)findViewById(R.id.textViewLastUpdated);
Intent intentReceived = getIntent();
final String strContent = intentReceived.getStringExtra("work_category");
etContent.setText(strContent);
Calendar now = Calendar.getInstance();
final String datetime = now.get(Calendar.DAY_OF_MONTH) + "/" +
(now.get(Calendar.MONTH) + 1) + "/" +
now.get(Calendar.YEAR) + " " +
now.get(Calendar.HOUR_OF_DAY) + ":" +
now.get(Calendar.MINUTE);
tvLastUpdated.setText(datetime);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strContentSaved = etContent.getText().toString();
SharedPreferences preferences = getSharedPreferences("category_work_saved", MODE_PRIVATE);
SharedPreferences.Editor prefEdit = preferences.edit();
prefEdit.putString("contentSaved", strContentSaved);
prefEdit.putString("date", datetime);
prefEdit.commit();
Toast.makeText(work_activity.this, "Content Saved Successfully", Toast.LENGTH_SHORT).show();
finish();
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences preferences = getSharedPreferences("category_work_saved", MODE_PRIVATE);
String contentSaved = preferences.getString("contentSaved", "");
etContent.setText(contentSaved);
String dateTime = preferences.getString("date","");
tvLastUpdated.setText(dateTime);
}
}
答案 0 :(得分:0)
看起来您可能正在尝试设置文字
etContent.setText(strContent);
没有strContent值......是这样吗?您正在发送“work_category”,但它可能是空值“”
答案 1 :(得分:0)
您的代码中存在一些严重问题。我在这里指出这些问题。
在您的MainActivity
中,您错误地比较了String
值。您需要使用equals()
方法而不是==
运算符比较来比较String
。
是的,尝试向work_activity
或school_activity
发送内容。
lvCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String category = (String) parent.getItemAtPosition(position);
if (category.equals("School")) {
Intent school = new Intent(view.getContext(), school_activity.class);
school.putExtra("school_category", "send something here");
startActivity(school);
}
if (category.equals("Work")) {
Intent work = new Intent(view.getContext(), work_activity.class);
work.putExtra("work_category", "send something here");
startActivity(work);
}
}
});
现在,在您的work_activity
中,您需要检查收到的null
的{{1}}值。
intent
检查String strContent = null;
Intent intentReceived = getIntent();
// Check if the intent received is null
if (intentReceived != null)
strContent = intentReceived.getStringExtra("work_category");
// Now check if the string content is null
if(strContent != null)
etContent.setText(strContent);
布局中EditText
的ID是activity_work_activity
的另一件事。 id需要匹配才能找到可能导致崩溃的editTextContent
。
答案 2 :(得分:0)
错误说,
引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence)'
这意味着null
正在返回id
。
我认为您忘了将editTextContent
EditText
添加到Editetext
,或者您在布局activity_work_activity.xml
中没有equals
。
同样如评论中建议的cricket007,使用if (category.equals("School")){
...
}
比较字符串如下,
<dependency>
<groupId>org.opensaml</groupId>
<artifactId>opensaml-saml-impl</artifactId>
<version>3.2.0</version>
</dependency>