Home Forum

App Inventor Button on Dialog crush app

Hi evryone whatsupp so thx for entering my qustion...
i have some activty and it conact to dialog
in this dialog i have some button
everything wroks allright until


buttonLoad.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {

Toast.makeText(contant.this, "informition entered secsfuly",Toast.LENGTH_SHORT ).show();

}
});

i dont now why its heppening, pls help
and have a nice day

by the way - sorry if my english not so good....;)

XML Code (dialog):
https://gist.github.com/anonymous/b723ff3b747fcdd613ab4b23bbbcfd8d


Java Code(activity):
https://gist.github.com/anonymous/ab998ad186daa464b44572848ead9a8d


[​IMG]
 

Related:

#1 avichay, Feb 12, 2018 Last edited: Feb 12, 2018
Since buttonLoad is actually inflate in dialog_custom, it should be initialized like this way

Code (Text):
  1. View mView = getLayoutInflater().inflate(R.layout.dialog_custom, null);
  2. Button buttonLoad = (Button) mView.findViewById(R.id.buttonLoad);
 
#2 wseng92, Feb 13, 2018

thx very much !

but now i have inther problem from some reson , maybe can u hale me with that too.

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


Code (Text):
  1.  
  2. @Override
  3. public boolean onOptionsItemSelected(MenuItem item) {
  4.     int id = item.getItemId();
  5.  
  6.     if(id == R.id.add_contant) {
  7.  
  8.         final EditText classEdit = (EditText) findViewById(R.id.classEdit);
  9.         final EditText gradeEdit = (EditText) findViewById(R.id.gradeEdit);
  10.  
  11.         AlertDialog.Builder  mBuilder = new AlertDialog.Builder(contant.this);
  12.         View mView = getLayoutInflater().inflate(R.layout.dialog_custom, null);
  13.         Button buttonLoad = (Button) mView.findViewById(R.id.buttonLoad);
  14.  
  15.         final int i =Integer.parseInt(gradeEdit.getText().toString());
  16.  
  17.         buttonLoad.setOnClickListener(new View.OnClickListener() {
  18.             @Override
  19.             public void onClick(View view) {
  20.  
  21.                 if (!classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty() && i<100 && i>0){
  22.                     Toast.makeText(contant.this, "מידע הוכנס בהצלחה",Toast.LENGTH_SHORT ).show();
  23.                 }
  24.                 else if (classEdit.getText().toString().isEmpty() && gradeEdit.getText().toString().isEmpty()){
  25.                     Toast.makeText(contant.this, "שני התאים ריקיים",Toast.LENGTH_SHORT ).show();
  26.                 }
  27.                 else if (!classEdit.getText().toString().isEmpty() && gradeEdit.getText().toString().isEmpty()){
  28.                     Toast.makeText(contant.this, "תא השיעור ריק",Toast.LENGTH_SHORT ).show();
  29.                 }
  30.                 else if (classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty()){
  31.                     Toast.makeText(contant.this, "תא הציון ריק",Toast.LENGTH_SHORT ).show();
  32.                 }
  33.                 else if (!classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty() ){
  34.                     Toast.makeText(contant.this, "תא הציון ריק",Toast.LENGTH_SHORT ).show();
  35.                 }
  36.                 else {
  37.                     Toast.makeText(contant.this, "שגיאה בהכנסת מידע",Toast.LENGTH_SHORT ).show();
  38.                 }
  39.             }
  40.         });
  41.  
  42.  
  43.  
  44.         mBuilder.setView(mView);
  45.         AlertDialog dialog = mBuilder.create();
  46.         dialog.show();
  47.  
  48.     }
  49.     return super.onOptionsItemSelected(item);
  50. }
  51. [COLOR=rgb(0, 0, 0)]

[/COLOR]
(sorry if u dont understand whats in the toast)

[​IMG]
 
#3 avichay, Feb 15, 2018 at 12:13 AM
From the information you've given, I'd say that one or both of these objects is null:

Code (Text):
  1.  
  2. final EditText classEdit = (EditText) findViewById(R.id.classEdit);
  3. final EditText gradeEdit = (EditText) findViewById(R.id.gradeEdit);
  4.  
Are you using the correct View ids (R.id.classEdit, R.id.gradeEdit) for these EditTexts?
 
#4 LV426, Feb 15, 2018 at 12:30 AM
Also, please show the full stack trace, not just the one line with the exception.
 
#5 LV426, Feb 15, 2018 at 12:33 AM
You may find this helpful

https://androidforums.com/threads/please-read-me-before-posting.987318/
 
#6 LV426, Feb 15, 2018 at 12:33 AM

but they must be final
 
#7 avichay, Feb 15, 2018 at 7:15 AM
Code (Text):
  1.  
  2. package com.avichay;
  3.  
  4. import android.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuInflater;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. public class contant extends AppCompatActivity   {
  16.  
  17.  
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_contant);
  23.  
  24.     }
  25.  
  26.     public boolean onCreateOptionsMenu(Menu menu) {
  27.  
  28.         MenuInflater menuInflater = getMenuInflater();
  29.         menuInflater.inflate(R.menu.actionbar_menu_contant, menu);
  30.         return super.onCreateOptionsMenu(menu);
  31.     }
  32.  
  33.  
  34.     @Override
  35.     public boolean onOptionsItemSelected(MenuItem item) {
  36.         int id = item.getItemId();
  37.  
  38.         if(id == R.id.add_contant) {
  39.  
  40.             final EditText classEdit = (EditText) findViewById(R.id.classEdit);
  41.             final EditText gradeEdit = (EditText) findViewById(R.id.gradeEdit);
  42.  
  43.             AlertDialog.Builder  mBuilder = new AlertDialog.Builder(contant.this);
  44.             View mView = getLayoutInflater().inflate(R.layout.dialog_custom, null);
  45.  
  46.  
  47.             Button buttonLoad = (Button) mView.findViewById(R.id.buttonLoad);
  48.  
  49.             //final int i =Integer.parseInt(gradeEdit.getText().toString());
  50.  
  51.  
  52.                 buttonLoad.setOnClickListener(new View.OnClickListener() {
  53.                     @Override
  54.                     public void onClick(View view) {
  55.  
  56.                         if (!classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty() /*&& i < 100 && i > 0*/) {
  57.                             Toast.makeText(contant.this, "מידע הוכנס בהצלחה", Toast.LENGTH_SHORT).show();
  58.                         } else if (classEdit.getText().toString().isEmpty() && gradeEdit.getText().toString().isEmpty()) {
  59.                             Toast.makeText(contant.this, "שני התאים ריקיים", Toast.LENGTH_SHORT).show();
  60.                         } else if (!classEdit.getText().toString().isEmpty() && gradeEdit.getText().toString().isEmpty()) {
  61.                             Toast.makeText(contant.this, "תא השיעור ריק", Toast.LENGTH_SHORT).show();
  62.                         } else if (classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty()) {
  63.                             Toast.makeText(contant.this, "תא הציון ריק", Toast.LENGTH_SHORT).show();
  64.                         } else if (!classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty()) {
  65.                             Toast.makeText(contant.this, "תא הציון ריק", Toast.LENGTH_SHORT).show();
  66.                         } else {
  67.                             Toast.makeText(contant.this, "שגיאה בהכנסת מידע", Toast.LENGTH_SHORT).show();
  68.                         }
  69.                     }
  70.                 });
  71.  
  72.  
  73.  
  74.  
  75.             mBuilder.setView(mView);
  76.             AlertDialog dialog = mBuilder.create();
  77.             dialog.show();
  78.  
  79.         }
  80.         return super.onOptionsItemSelected(item);
  81.     }
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
 
#8 avichay, Feb 15, 2018 at 7:16 AM
EditText inflate dialog_custom.xml too.

Code (Text):
  1.  final EditText classEdit = (EditText)view.findViewById(R.id.classEdit);
  2.  final EditText  gradeEdit = (EditText)view.findViewById(R.id.gradeEdit);
 
#9 wseng92, Feb 15, 2018 at 7:27 AM
That's not going to help you if the View ids return a null object.
 
#10 LV426, Feb 15, 2018 at 7:51 AM
thx i get it
 
#11 avichay, Feb 15, 2018 at 8:25 AM
thx
 
#12 avichay, Feb 15, 2018 at 8:25 AM