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

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

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

问题

以下是翻译好的内容:

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

这是我的 XML 代码:

  1. <EditText
  2. android:id="@+id/tvDate"
  3. android:layout_width="174dp"
  4. android:layout_height="40dp"
  5. android:layout_marginTop="24dp"
  6. android:ems="10"
  7. android:inputType="date|time"
  8. app:layout_constraintEnd_toEndOf="@+id/cbr"
  9. app:layout_constraintHorizontal_bias="0.0"
  10. app:layout_constraintStart_toStartOf="@+id/cbr"
  11. app:layout_constraintTop_toBottomOf="@+id/cbr" />

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

  1. protected void onCreate(Bundle savedInstanceState) {
  2. nama = (EditText) findViewById(R.id.name);
  3. /*email=(Switch)findViewById(R.id.switch1);*/
  4. wsomething = (EditText) findViewById(R.id.wSome);
  5. btnSave = (Button) findViewById(R.id.btnsave);
  6. btnClear = (Button) findViewById(R.id.btnclear);
  7. btnChange = (Button) findViewById(R.id.btnImg);
  8. radLaki = (RadioButton) findViewById(R.id.rbl);
  9. radPerempuan = (RadioButton) findViewById(R.id.rbp);
  10. chbSekolah = (CheckBox) findViewById(R.id.cbs);
  11. chbRumah = (CheckBox) findViewById(R.id.cbr);
  12. tvDateRes = (EditText) findViewById(R.id.tvDate);
  13. btnPick = (Button) findViewById(R.id.btnTime);
  14. ivImage = (ImageView) findViewById(R.id.Img);
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_input_user);
  17. Button btnTime = (Button) findViewById(R.id.btnTime);
  18. dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
  19. btnTime.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. showDateDialog();
  23. }
  24. });
  25. }

这是方法:

  1. private void showDateDialog() {
  2. Calendar newcalendar = Calendar.getInstance();
  3. DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
  4. @Override
  5. public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  6. Calendar newDate = Calendar.getInstance();
  7. newDate.set(year, monthOfYear, dayOfMonth);
  8. tvDateRes.setText(dateFormatter.format(newDate.getTime()));
  9. }
  10. }, newcalendar.get(Calendar.YEAR), newcalendar.get(Calendar.MONTH), newcalendar.get(Calendar.DAY_OF_MONTH));
  11. datePickerDialog.show();
  12. }

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

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 :

  1. &lt;EditText
  2. android:id=&quot;@+id/tvDate&quot;
  3. android:layout_width=&quot;174dp&quot;
  4. android:layout_height=&quot;40dp&quot;
  5. android:layout_marginTop=&quot;24dp&quot;
  6. android:ems=&quot;10&quot;
  7. android:inputType=&quot;date|time&quot;
  8. app:layout_constraintEnd_toEndOf=&quot;@+id/cbr&quot;
  9. app:layout_constraintHorizontal_bias=&quot;0.0&quot;
  10. app:layout_constraintStart_toStartOf=&quot;@+id/cbr&quot;
  11. app:layout_constraintTop_toBottomOf=&quot;@+id/cbr&quot; /&gt;

and this is my InputUser.java file code:

  1. protected void onCreate(Bundle savedInstanceState) {
  2. nama=(EditText)findViewById(R.id.name);
  3. /*email=(Switch)findViewById(R.id.switch1);*/
  4. wsomething=(EditText)findViewById(R.id.wSome);
  5. btnSave=(Button)findViewById(R.id.btnsave);
  6. btnClear=(Button)findViewById(R.id.btnclear);
  7. btnChange=(Button)findViewById(R.id.btnImg);
  8. radLaki=(RadioButton)findViewById(R.id.rbl);
  9. radPerempuan=(RadioButton)findViewById(R.id.rbp);
  10. chbSekolah=(CheckBox)findViewById(R.id.cbs);
  11. chbRumah=(CheckBox)findViewById(R.id.cbr);
  12. tvDateRes=(EditText)findViewById(R.id.tvDate);
  13. btnPick=(Button)findViewById(R.id.btnTime);
  14. ivImage=(ImageView)findViewById(R.id.Img);
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_input_user);
  17. Button btnTime = (Button)findViewById(R.id.btnTime);
  18. dateFormatter = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;, Locale.US);
  19. btnTime.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. showDateDialog();
  23. }
  24. });
  25. }

and this is the method:

  1. private void showDateDialog()
  2. {
  3. Calendar newcalendar= Calendar.getInstance();
  4. DatePickerDialog datePickerDialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
  5. @Override
  6. public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  7. Calendar newDate = Calendar.getInstance();
  8. newDate.set(year, monthOfYear, dayOfMonth);
  9. tvDateRes.setText(dateFormatter.format(newDate.getTime()));
  10. }
  11. }, newcalendar.get(Calendar.YEAR), newcalendar.get(Calendar.MONTH), newcalendar.get(Calendar.DAY_OF_MONTH));
  12. datePickerDialog.show();
  13. }

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)

代码应该如下所示:

  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. setContentView(R.layout.xxx);
  4. nama = (EditText) findViewById(R.id.name);
  5. /*email=(Switch)findViewById(R.id.switch1);*/
  6. wsomething = (EditText) findViewById(R.id.wSome);
  7. btnSave = (Button) findViewById(R.id.btnsave);
  8. btnClear = (Button) findViewById(R.id.btnclear);
  9. btnChange = (Button) findViewById(R.id.btnImg);
  10. radLaki = (RadioButton) findViewById(R.id.rbl);
  11. radPerempuan = (RadioButton) findViewById(R.id.rbp);
  12. chbSekolah = (CheckBox) findViewById(R.id.cbs);
  13. chbRumah = (CheckBox) findViewById(R.id.cbr);
  14. tvDateRes = (EditText) findViewById(R.id.tvDate);
  15. btnPick = (Button) findViewById(R.id.btnTime);
  16. ivImage = (ImageView) findViewById(R.id.Img);
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_input_user);
  19. Button btnTime = (Button) findViewById(R.id.btnTime);
  20. dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
  21. btnTime.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. showDateDialog();
  25. }
  26. });
  27. }
英文:

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

The code should be like this :

  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. setContentView(R.layout.xxx);
  4. nama=(EditText)findViewById(R.id.name);
  5. /*email=(Switch)findViewById(R.id.switch1);*/
  6. wsomething=(EditText)findViewById(R.id.wSome);
  7. btnSave=(Button)findViewById(R.id.btnsave);
  8. btnClear=(Button)findViewById(R.id.btnclear);
  9. btnChange=(Button)findViewById(R.id.btnImg);
  10. radLaki=(RadioButton)findViewById(R.id.rbl);
  11. radPerempuan=(RadioButton)findViewById(R.id.rbp);
  12. chbSekolah=(CheckBox)findViewById(R.id.cbs);
  13. chbRumah=(CheckBox)findViewById(R.id.cbr);
  14. tvDateRes=(EditText)findViewById(R.id.tvDate);
  15. btnPick=(Button)findViewById(R.id.btnTime);
  16. ivImage=(ImageView)findViewById(R.id.Img);
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_input_user);
  19. Button btnTime = (Button)findViewById(R.id.btnTime);
  20. dateFormatter = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;, Locale.US);
  21. btnTime.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. showDateDialog();
  25. }
  26. });
  27. }

答案2

得分: 0

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

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

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

英文:

Move these two lines to before you set your views

  1. super.onCreate(savedInstanceState);
  2. 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等。

请重新排列您的代码。

  1. private EditText name;
  2. private EditText wsomething;
  3. private Button btnSave;
  4. private Button btnChange;
  5. //还有其他视图...
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_input_user);
  9. name = findViewById(R.id.name);
  10. /*email = findViewById(R.id.switch1);*/
  11. wsomething = findViewById(R.id.wSome);
  12. btnSave = findViewById(R.id.btnsave);
  13. btnClear = findViewById(R.id.btnclear);
  14. btnChange = findViewById(R.id.btnImg);
  15. radLaki = findViewById(R.id.rbl);
  16. radPerempuan = findViewById(R.id.rbp);
  17. chbSekolah = findViewById(R.id.cbs);
  18. chbRumah = findViewById(R.id.cbr);
  19. tvDateRes = findViewById(R.id.tvDate);
  20. btnPick = findViewById(R.id.btnTime);
  21. ivImage = findViewById(R.id.Img);
  22. Button btnTime = findViewById(R.id.btnTime);
  23. //然后尝试您的代码...
英文:

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.

  1. private EditText name;
  2. private EditText wsomething;
  3. private Button btnSave;
  4. private Button btnChange;
  5. //all other views also...
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_input_user);
  9. name=(EditText)findViewById(R.id.name);
  10. /*email=(Switch)findViewById(R.id.switch1);*/
  11. wsomething=(EditText)findViewById(R.id.wSome);
  12. btnSave=(Button)findViewById(R.id.btnsave);
  13. btnClear=(Button)findViewById(R.id.btnclear);
  14. btnChange=(Button)findViewById(R.id.btnImg);
  15. radLaki=(RadioButton)findViewById(R.id.rbl);
  16. radPerempuan=(RadioButton)findViewById(R.id.rbp);
  17. chbSekolah=(CheckBox)findViewById(R.id.cbs);
  18. chbRumah=(CheckBox)findViewById(R.id.cbr);
  19. tvDateRes=(EditText)findViewById(R.id.tvDate);
  20. btnPick=(Button)findViewById(R.id.btnTime);
  21. ivImage=(ImageView)findViewById(R.id.Img);
  22. Button btnTime = (Button)findViewById(R.id.btnTime);
  23. //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:

确定