我的安卓项目在选择日期时总是崩溃/强制关闭。

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

My android project always get crash/force close when i pick date

问题

以下是翻译好的内容:

我的 Android 项目在选择某个日期时崩溃/强制关闭。

这是我的 XML 代码:

<EditText
    android:id="@+id/tvDate"
    android:layout_width="174dp"
    android:layout_height="40dp"
    android:layout_marginTop="24dp"
    android:ems="10"
    android:inputType="date|time"
    app:layout_constraintEnd_toEndOf="@+id/cbr"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/cbr"
    app:layout_constraintTop_toBottomOf="@+id/cbr" />

这是我的 InputUser.java 文件代码:

protected void onCreate(Bundle savedInstanceState) {

    nama = (EditText) findViewById(R.id.name);
    /*email=(Switch)findViewById(R.id.switch1);*/
    wsomething = (EditText) findViewById(R.id.wSome);
    btnSave = (Button) findViewById(R.id.btnsave);
    btnClear = (Button) findViewById(R.id.btnclear);
    btnChange = (Button) findViewById(R.id.btnImg);
    radLaki = (RadioButton) findViewById(R.id.rbl);
    radPerempuan = (RadioButton) findViewById(R.id.rbp);
    chbSekolah = (CheckBox) findViewById(R.id.cbs);
    chbRumah = (CheckBox) findViewById(R.id.cbr);
    tvDateRes = (EditText) findViewById(R.id.tvDate);
    btnPick = (Button) findViewById(R.id.btnTime);
    ivImage = (ImageView) findViewById(R.id.Img);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_user);
    Button btnTime = (Button) findViewById(R.id.btnTime);

    dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
    btnTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDateDialog();
        }
    });

}

这是方法:

private void showDateDialog() {
    Calendar newcalendar = Calendar.getInstance();
    DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            tvDateRes.setText(dateFormatter.format(newDate.getTime()));
        }
    }, newcalendar.get(Calendar.YEAR), newcalendar.get(Calendar.MONTH), newcalendar.get(Calendar.DAY_OF_MONTH));
    datePickerDialog.show();
}

我不知道为什么,但总是显示以下错误:

java.lang.NullPointerException: 尝试调用虚拟方法 'void android.widget.EditText.setText(java.lang.CharSequence)' 时出现空对象引用
堆栈跟踪:java.lang.NullPointerException: 尝试调用虚拟方法 'void android.widget.EditText.setText(java.lang.CharSequence)' 时出现空对象引用
at com.example.belajarlayout.InputUser$2.onDateSet(InputUser.java:126)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:171)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6739)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:859)

你们能帮我吗?

英文:

my android project get crash/force close when i pick some date

this is my xml code :

&lt;EditText
android:id=&quot;@+id/tvDate&quot;
android:layout_width=&quot;174dp&quot;
android:layout_height=&quot;40dp&quot;
android:layout_marginTop=&quot;24dp&quot;
android:ems=&quot;10&quot;
android:inputType=&quot;date|time&quot;
app:layout_constraintEnd_toEndOf=&quot;@+id/cbr&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/cbr&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/cbr&quot; /&gt;

and this is my InputUser.java file code:

 protected void onCreate(Bundle savedInstanceState) {
nama=(EditText)findViewById(R.id.name);
/*email=(Switch)findViewById(R.id.switch1);*/
wsomething=(EditText)findViewById(R.id.wSome);
btnSave=(Button)findViewById(R.id.btnsave);
btnClear=(Button)findViewById(R.id.btnclear);
btnChange=(Button)findViewById(R.id.btnImg);
radLaki=(RadioButton)findViewById(R.id.rbl);
radPerempuan=(RadioButton)findViewById(R.id.rbp);
chbSekolah=(CheckBox)findViewById(R.id.cbs);
chbRumah=(CheckBox)findViewById(R.id.cbr);
tvDateRes=(EditText)findViewById(R.id.tvDate);
btnPick=(Button)findViewById(R.id.btnTime);
ivImage=(ImageView)findViewById(R.id.Img);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_user);
Button btnTime = (Button)findViewById(R.id.btnTime);
dateFormatter = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;, Locale.US);
btnTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDateDialog();
}
});
}

and this is the method:

private void showDateDialog()
{
Calendar newcalendar= Calendar.getInstance();
DatePickerDialog datePickerDialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
tvDateRes.setText(dateFormatter.format(newDate.getTime()));
}
}, newcalendar.get(Calendar.YEAR), newcalendar.get(Calendar.MONTH), newcalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}

i dont know how but its always said

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
stacktrace: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.example.belajarlayout.InputUser$2.onDateSet(InputUser.java:126)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:171)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6739)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:859)

can you guys help me?

答案1

得分: 0

你没有在onCreate函数中设置setContentView(R.layout.xxx)

代码应该如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.xxx);

    nama = (EditText) findViewById(R.id.name);
    /*email=(Switch)findViewById(R.id.switch1);*/
    wsomething = (EditText) findViewById(R.id.wSome);
    btnSave = (Button) findViewById(R.id.btnsave);
    btnClear = (Button) findViewById(R.id.btnclear);
    btnChange = (Button) findViewById(R.id.btnImg);
    radLaki = (RadioButton) findViewById(R.id.rbl);
    radPerempuan = (RadioButton) findViewById(R.id.rbp);
    chbSekolah = (CheckBox) findViewById(R.id.cbs);
    chbRumah = (CheckBox) findViewById(R.id.cbr);
    tvDateRes = (EditText) findViewById(R.id.tvDate);
    btnPick = (Button) findViewById(R.id.btnTime);
    ivImage = (ImageView) findViewById(R.id.Img);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_user);
    Button btnTime = (Button) findViewById(R.id.btnTime);

    dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
    btnTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDateDialog();
        }
    });
}
英文:

You didn't set setContentView(R.layout.xxx) in function onCreate.

The code should be like this :

@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.xxx);
nama=(EditText)findViewById(R.id.name);
/*email=(Switch)findViewById(R.id.switch1);*/
wsomething=(EditText)findViewById(R.id.wSome);
btnSave=(Button)findViewById(R.id.btnsave);
btnClear=(Button)findViewById(R.id.btnclear);
btnChange=(Button)findViewById(R.id.btnImg);
radLaki=(RadioButton)findViewById(R.id.rbl);
radPerempuan=(RadioButton)findViewById(R.id.rbp);
chbSekolah=(CheckBox)findViewById(R.id.cbs);
chbRumah=(CheckBox)findViewById(R.id.cbr);
tvDateRes=(EditText)findViewById(R.id.tvDate);
btnPick=(Button)findViewById(R.id.btnTime);
ivImage=(ImageView)findViewById(R.id.Img);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_user);
Button btnTime = (Button)findViewById(R.id.btnTime);
dateFormatter = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;, Locale.US);
btnTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDateDialog();
}
});
}

答案2

得分: 0

将这两行代码移到设置视图之前:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_user);

另外,我建议您在以后使用 View Binding 来避免这种错误。另外一件事是您不再需要使用 findViewById 进行类型转换。

英文:

Move these two lines to before you set your views

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_user);

On the side, I suggest you use View Binding to avoid such errors in the future. Another thing is you don't need to case anymore with findViewById.

答案3

得分: 0

您在调用super.onCreate(savedInstanceState);setContentView(R.layout.activity_input_user);之前不能调用findViewById。您还必须创建您的视图对象,例如TextView等。

请重新排列您的代码。

private EditText name;
private EditText wsomething;
private Button btnSave;
private Button btnChange;
//还有其他视图...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_user);

    name = findViewById(R.id.name);
    /*email = findViewById(R.id.switch1);*/
    wsomething = findViewById(R.id.wSome);
    btnSave = findViewById(R.id.btnsave);
    btnClear = findViewById(R.id.btnclear);
    btnChange = findViewById(R.id.btnImg);
    radLaki = findViewById(R.id.rbl);
    radPerempuan = findViewById(R.id.rbp);
    chbSekolah = findViewById(R.id.cbs);
    chbRumah = findViewById(R.id.cbr);
    tvDateRes = findViewById(R.id.tvDate);
    btnPick = findViewById(R.id.btnTime);
    ivImage = findViewById(R.id.Img);
    Button btnTime = findViewById(R.id.btnTime);

    //然后尝试您的代码...
英文:

You can't call findViewById before calling super.onCreate(savedInstanceState); and setContentView(R.layout.activity_input_user); And you must create object of your views. For example TextView etc.

so rearrange your codes.

private EditText name;
private EditText wsomething;
private Button btnSave;
private Button btnChange;
//all other views also...

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_user);

    name=(EditText)findViewById(R.id.name);
    /*email=(Switch)findViewById(R.id.switch1);*/
    wsomething=(EditText)findViewById(R.id.wSome);
    btnSave=(Button)findViewById(R.id.btnsave);
    btnClear=(Button)findViewById(R.id.btnclear);
    btnChange=(Button)findViewById(R.id.btnImg);
    radLaki=(RadioButton)findViewById(R.id.rbl);
    radPerempuan=(RadioButton)findViewById(R.id.rbp);
    chbSekolah=(CheckBox)findViewById(R.id.cbs);
    chbRumah=(CheckBox)findViewById(R.id.cbr);
    tvDateRes=(EditText)findViewById(R.id.tvDate);
    btnPick=(Button)findViewById(R.id.btnTime);
    ivImage=(ImageView)findViewById(R.id.Img);
    Button btnTime = (Button)findViewById(R.id.btnTime);

    //Then try your codes...

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

发表评论

匿名网友

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

确定