英文:
"java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object..."
问题
package com.example.as3;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class AddRemainDaysActivity extends AppCompatActivity {
TextView textviewAsgName;
EditText editTextRemaindDays;
Button buttonAdd;
ListView listViewRemainDays;
DatabaseReference databaseRemainDays;
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.add_remain_days);
textviewAsgName = (TextView) findViewById(R.id.textViewAsgName);
editTextRemaindDays = (EditText) findViewById(R.id.editTextRemaindDays);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
listViewRemainDays = (ListView) findViewById(R.id.listViewRemainDays);
Intent intent = getIntent();
String id = intent.getStringExtra(MainActivity.ASSIGNMENT_ID);
String topic = intent.getStringExtra(MainActivity.ASSIGNMENT_TOPIC);
textviewAsgName.setText(topic);
databaseRemainDays = FirebaseDatabase.getInstance().getReference("remainDays").child(id);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveremainDays();
}
});
}
private void saveremainDays(){
String remainDaysname = editTextRemaindDays.getText().toString().trim();
if(!TextUtils.isEmpty(remainDaysname)) {
String id = databaseRemainDays.push().getKey();
remainDays RemainDays = new remainDays(id, remainDaysname);
databaseRemainDays.child(id).setValue(RemainDays);
Toast.makeText(this, "Remain days added :)", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "You must insert remaining days to proceed!", Toast.LENGTH_LONG).show();
}
}
}
package com.example.as3;
public class remainDays {
private String remainDaysID;
private String remainDaysname;
public remainDays() {
}
public remainDays(String remainDaysID, String remainDaysname) {
this.remainDaysID = remainDaysID;
this.remainDaysname = remainDaysname;
}
public String getRemainDaysID() {
return remainDaysID;
}
public String getRemainDaysname() {
return remainDaysname;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Activity_add_remaindays"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AddRemainDaysActivity">
<TextView
android:id="@+id/textViewAsgName"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<EditText
android:id="@+id/editTextRemainDays"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter remaining days"
android:inputType="textPersonName"
android:layout_marginTop="20dp" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:text="Add" />
<TextView
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Assignment" />
<ListView
android:id="@+id/listViewRemainDays"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
英文:
I make a code to add the remaining days but it shows some off my code are error and I am new with Android Studio.
I also upload the logcat error, please click here:
Logcat
[AddRemainDaysActivity.java]
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class AddRemainDaysActivity extends AppCompatActivity {
TextView textviewAsgName;
EditText editTextRemaindDays;
Button buttonAdd;
ListView listViewRemainDays;
DatabaseReference databaseRemainDays;
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.add_remain_days);
textviewAsgName = (TextView) findViewById(R.id.textViewAsgName);
editTextRemaindDays = (EditText) findViewById(R.id.editTextRemaindDays);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
listViewRemainDays = (ListView) findViewById(R.id.listViewRemainDays);
Intent intent = getIntent();
String id = intent.getStringExtra(MainActivity.ASSIGNMENT_ID);
String topic = intent.getStringExtra(MainActivity.ASSIGNMENT_TOPIC);
textviewAsgName.setText(topic);
databaseRemainDays = FirebaseDatabase.getInstance().getReference("remainDays").child(id);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveremainDays();
}
});
}
private void saveremainDays(){
String remainDaysname = editTextRemaindDays.getText().toString().trim();
if(!TextUtils.isEmpty(remainDaysname)) {
String id = databaseRemainDays.push().getKey();
remainDays RemainDays = new remainDays(id, remainDaysname);
databaseRemainDays.child(id).setValue(RemainDays);
Toast.makeText(this,"Remain days added :)", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "You must insert remaining days to proceed!", Toast.LENGTH_LONG).show();
}
}
}````
[remainDays.java]
package com.example.as3;
public class remainDays {
private String remainDaysID;
private String remainDaysname;
public remainDays() {
}
public remainDays(String remainDaysID, String remainDaysname) {
this.remainDaysID = remainDaysID;
this.remainDaysname = remainDaysname;
}
public String getRemainDaysID() {
return remainDaysID;
}
public String getRemainDaysname() {
return remainDaysname;
}
}````
This is the XML file:
[add_remain_days_xml]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Activity_add_remaindays"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AddRemainDaysActivity">
<TextView
android:id="@+id/textViewAsgName"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<EditText
android:id="@+id/editTextRemainDays"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter remaining days"
android:inputType="textPersonName"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/buttonAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:text="Add" />
<TextView
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Assignment"/>
<ListView
android:id="@+id/listViewRemainDays"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>````
</details>
# 答案1
**得分**: 0
你的 `editTextRemaindDays` 视图没有被初始化,所以它抛出了空指针异常。从你的导入情况来看,你没有像这样导入你的资源文件。
import yourPackage.R;
请仔细检查你是否使用正确的ID `editTextRemaindDays` 访问了视图,在xml文件中提供了正确的ID。由于你没有附上你的xml文件,所以我无法看到你提供了什么ID。
<details>
<summary>英文:</summary>
your `editTextRemaindDays` view is not getting initialised, that's why it is throwing NullPointerException. when i see your imports you are not importing your resource file like this.
import youPackage.R;
Please check properly you are accessing `editTextRemaindDays` view with proper id `editTextRemaindDays` what ever you have given in xml file, as you have not attached your xml bacause of this i cann't see what id you have given
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论