• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

App Inventor Button on Dialog crush app

avichay

Newbie
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


255.gif
 
Last edited:
Since buttonLoad is actually inflate in dialog_custom, it should be initialized like this way

Code:
View mView = getLayoutInflater().inflate(R.layout.dialog_custom, null);
Button buttonLoad = (Button) mView.findViewById(R.id.buttonLoad);
 
Since buttonLoad is actually inflate in dialog_custom, it should be initialized like this way

Code:
View mView = getLayoutInflater().inflate(R.layout.dialog_custom, null);
Button buttonLoad = (Button) mView.findViewById(R.id.buttonLoad);


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:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.add_contant) {

        final EditText classEdit = (EditText) findViewById(R.id.classEdit);
        final EditText gradeEdit = (EditText) findViewById(R.id.gradeEdit);

        AlertDialog.Builder  mBuilder = new AlertDialog.Builder(contant.this);
        View mView = getLayoutInflater().inflate(R.layout.dialog_custom, null);
        Button buttonLoad = (Button) mView.findViewById(R.id.buttonLoad);

        final int i =Integer.parseInt(gradeEdit.getText().toString());

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

                if (!classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty() && i<100 && i>0){
                    Toast.makeText(contant.this, "מידע הוכנס בהצלחה",Toast.LENGTH_SHORT ).show();
                }
                else if (classEdit.getText().toString().isEmpty() && gradeEdit.getText().toString().isEmpty()){
                    Toast.makeText(contant.this, "שני התאים ריקיים",Toast.LENGTH_SHORT ).show();
                }
                else if (!classEdit.getText().toString().isEmpty() && gradeEdit.getText().toString().isEmpty()){
                    Toast.makeText(contant.this, "תא השיעור ריק",Toast.LENGTH_SHORT ).show();
                }
                else if (classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty()){
                    Toast.makeText(contant.this, "תא הציון ריק",Toast.LENGTH_SHORT ).show();
                }
                else if (!classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty() ){
                    Toast.makeText(contant.this, "תא הציון ריק",Toast.LENGTH_SHORT ).show();
                }
                else {
                    Toast.makeText(contant.this, "שגיאה בהכנסת מידע",Toast.LENGTH_SHORT ).show();
                }
            }
        });



        mBuilder.setView(mView);
        AlertDialog dialog = mBuilder.create();
        dialog.show();

    }
    return super.onOptionsItemSelected(item);
}
[COLOR=rgb(0, 0, 0)]
[/COLOR]
(sorry if u dont understand whats in the toast)

giphy.gif
 
From the information you've given, I'd say that one or both of these objects is null:

Code:
final EditText classEdit = (EditText) findViewById(R.id.classEdit);
final EditText gradeEdit = (EditText) findViewById(R.id.gradeEdit);

Are you using the correct View ids (R.id.classEdit, R.id.gradeEdit) for these EditTexts?
 
Also, please show the full stack trace, not just the one line with the exception.
 
From the information you've given, I'd say that one or both of these objects is null:

Code:
final EditText classEdit = (EditText) findViewById(R.id.classEdit);
final EditText gradeEdit = (EditText) findViewById(R.id.gradeEdit);

Are you using the correct View ids (R.id.classEdit, R.id.gradeEdit) for these EditTexts?


but they must be final
 
Also, please show the full stack trace, not just the one line with the exception.

Code:
package com.avichay;

import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class contant extends AppCompatActivity   {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contant);

    }

    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.actionbar_menu_contant, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.add_contant) {

            final EditText classEdit = (EditText) findViewById(R.id.classEdit);
            final EditText gradeEdit = (EditText) findViewById(R.id.gradeEdit);

            AlertDialog.Builder  mBuilder = new AlertDialog.Builder(contant.this);
            View mView = getLayoutInflater().inflate(R.layout.dialog_custom, null);


            Button buttonLoad = (Button) mView.findViewById(R.id.buttonLoad);

            //final int i =Integer.parseInt(gradeEdit.getText().toString());


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

                        if (!classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty() /*&& i < 100 && i > 0*/) {
                            Toast.makeText(contant.this, "מידע הוכנס בהצלחה", Toast.LENGTH_SHORT).show();
                        } else if (classEdit.getText().toString().isEmpty() && gradeEdit.getText().toString().isEmpty()) {
                            Toast.makeText(contant.this, "שני התאים ריקיים", Toast.LENGTH_SHORT).show();
                        } else if (!classEdit.getText().toString().isEmpty() && gradeEdit.getText().toString().isEmpty()) {
                            Toast.makeText(contant.this, "תא השיעור ריק", Toast.LENGTH_SHORT).show();
                        } else if (classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty()) {
                            Toast.makeText(contant.this, "תא הציון ריק", Toast.LENGTH_SHORT).show();
                        } else if (!classEdit.getText().toString().isEmpty() && !gradeEdit.getText().toString().isEmpty()) {
                            Toast.makeText(contant.this, "תא הציון ריק", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(contant.this, "שגיאה בהכנסת מידע", Toast.LENGTH_SHORT).show();
                        }
                    }
                });




            mBuilder.setView(mView);
            AlertDialog dialog = mBuilder.create();
            dialog.show();

        }
        return super.onOptionsItemSelected(item);
    }


}
 
EditText inflate dialog_custom.xml too.

Code:
 final EditText classEdit = (EditText)view.findViewById(R.id.classEdit);
 final EditText  gradeEdit = (EditText)view.findViewById(R.id.gradeEdit);
 
Back
Top Bottom