1. Download our Official Android App: Forums for Android!

App Inventor Button on Dialog crush app

Discussion in 'Android Development' started by avichay, Feb 12, 2018.

  1. avichay

    avichay Newbie
    Thread Starter
    Rank:
     #685
    Points:
    26
    Posts:
    13
    Joined:
    Jan 7, 2018

    Jan 7, 2018
    13
    1
    26
    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]
     

    Advertisement

    #1 avichay, Feb 12, 2018
    Last edited: Feb 12, 2018
  2. wseng92

    wseng92 Member
    Rank:
     #763
    Points:
    18
    Posts:
    48
    Joined:
    Oct 26, 2015

    Oct 26, 2015
    48
    12
    18
    Male
    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);
     
    avichay likes this.
  3. avichay

    avichay Newbie
    Thread Starter
    Rank:
     #685
    Points:
    26
    Posts:
    13
    Joined:
    Jan 7, 2018

    Jan 7, 2018
    13
    1
    26

    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]
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #19
    Points:
    1,238
    Posts:
    5,893
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    5,893
    7,937
    1,238
    Male
    Software developer
    South West of England
    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?
     
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #19
    Points:
    1,238
    Posts:
    5,893
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    5,893
    7,937
    1,238
    Male
    Software developer
    South West of England
    Also, please show the full stack trace, not just the one line with the exception.
     
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #19
    Points:
    1,238
    Posts:
    5,893
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    5,893
    7,937
    1,238
    Male
    Software developer
    South West of England
  7. avichay

    avichay Newbie
    Thread Starter
    Rank:
     #685
    Points:
    26
    Posts:
    13
    Joined:
    Jan 7, 2018

    Jan 7, 2018
    13
    1
    26

    but they must be final
     
  8. avichay

    avichay Newbie
    Thread Starter
    Rank:
     #685
    Points:
    26
    Posts:
    13
    Joined:
    Jan 7, 2018

    Jan 7, 2018
    13
    1
    26
    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.  
     
  9. wseng92

    wseng92 Member
    Rank:
     #763
    Points:
    18
    Posts:
    48
    Joined:
    Oct 26, 2015

    Oct 26, 2015
    48
    12
    18
    Male
    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);
     
    avichay likes this.
  10. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #19
    Points:
    1,238
    Posts:
    5,893
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    5,893
    7,937
    1,238
    Male
    Software developer
    South West of England
    That's not going to help you if the View ids return a null object.
     
    avichay likes this.
  11. avichay

    avichay Newbie
    Thread Starter
    Rank:
     #685
    Points:
    26
    Posts:
    13
    Joined:
    Jan 7, 2018

    Jan 7, 2018
    13
    1
    26
    thx i get it
     
  12. avichay

    avichay Newbie
    Thread Starter
    Rank:
     #685
    Points:
    26
    Posts:
    13
    Joined:
    Jan 7, 2018

    Jan 7, 2018
    13
    1
    26
    thx
     
    wseng92 likes this.

Share This Page

Loading...