"java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object…"

huangapple go评论77阅读模式
英文:

"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(&quot;remainDays&quot;).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,&quot;Remain days added :)&quot;, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, &quot;You must insert remaining days to proceed!&quot;, 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]

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/Activity_add_remaindays&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.AddRemainDaysActivity&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textViewAsgName&quot;
android:textAppearance=&quot;@style/TextAppearance.AppCompat.Large&quot;
android:textAlignment=&quot;center&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/editTextRemainDays&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:ems=&quot;10&quot;
android:hint=&quot;Enter remaining days&quot;
android:inputType=&quot;textPersonName&quot;
android:layout_marginTop=&quot;20dp&quot;/&gt;
&lt;Button
android:id=&quot;@+id/buttonAdd&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;22dp&quot;
android:text=&quot;Add&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView&quot;
android:textAppearance=&quot;@style/TextAppearance.AppCompat.Large&quot;
android:textAlignment=&quot;center&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Assignment&quot;/&gt;
&lt;ListView
android:id=&quot;@+id/listViewRemainDays&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;/&gt;
&lt;/LinearLayout&gt;````
</details>
# 答案1
**得分**: 0
你的 `editTextRemaindDays` 视图没有被初始化,所以它抛出了空指针异常。从你的导入情况来看,你没有像这样导入你的资源文件。
import yourPackage.R;
请仔细检查你是否使用正确的ID `editTextRemaindDays` 访问了视图,在xml文件中提供了正确的ID。由于你没有附上你的xml文件,所以我无法看到你提供了什么ID。
<details>
<summary>英文:</summary>
your `editTextRemaindDays` view is not getting initialised, that&#39;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&#39;t see what id you have given
</details>

huangapple
  • 本文由 发表于 2020年5月3日 22:33:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61576177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定