How to take parameter from inside the popup?

Asked

Viewed 77 times

1

I have a screen where I select the pages, and when I clicked on the page opens a pop up where I can type the value of the pages instead of going by. (In case I have 60)

the page is nothing more than the ID of my values in the database, and I just write what I read in the ID.

when I run the code it error on that line

valor = etpage.getText().toString();

where value is a global scope variable of type String and the editable field it receives only accepts number.

the code that generates the pop up

    @Override
public void onWindowFocusChanged(boolean hasFocus) {
    int[] location = new int[2];
    Button button = (Button) findViewById(R.id.tv2);

    button.getLocationOnScreen(location);

    p.x = location[0];
    p.y = location[1];
}

@SuppressWarnings("deprecation")
public void showPopup(final Activity context, Point p) {
    int popupWidth = 300;
    int popupHeight = 400;
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
    LayoutInflater layoutInflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
    final PopupWindow popup = new PopupWindow(context);
    popup.setContentView(layout);
    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);
    popup.setFocusable(true);

    popup.setBackgroundDrawable(new BitmapDrawable());
    popup.showAtLocation(layout, 
            Gravity.NO_GRAVITY, 1, 1 );
    Button close = (Button) layout.findViewById(R.id.buttonir);
    close.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            valor = etpage.getText().toString();
            popup.dismiss();


        }
    });

}

}tela debug

error screen : Unknown error: Java.lang.Nullpointerexception

public class MainActivity extends ActionBarActivity {
SQLiteDatabase Banco = null;
Cursor cursor;
ToggleButton bt01,bt02,bt03,bt04,bt05,bt06,bt07,
bt08,bt09,bt10,bt11,bt12,bt13,bt14,bt15;
Button btnext,btprevious, pageview,btdescricao, botaopage;
EditText etpage,etpage2;
int page=1;
String valor = "1";
int progress;
private Point p = new Point();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.activity_main);
    Log.i("main", "Executou o onCreate();");
    abrebanco();
iniciaobjetos();
    Intent ipage = getIntent();
    page = ipage.getIntExtra("pagina2", 1);
    if (page<10)
        pageview.setText("0"+String.valueOf(page));
    else pageview.setText(String.valueOf(page));
    buscardados();
    setacursor(page);
    escrevebotao();

    btprevious.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (page>1)
                page--;
            else
                page=60;
            setacursor(page);
            escrevebotao();
            if (page<10)
                pageview.setText("0"+String.valueOf(page));
            else pageview.setText(String.valueOf(page));
        }

    });
    btprevious.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {



            if (page>1)
                page--;
            else
                page=60;
            setacursor(page);
            escrevebotao();
            if (page<10)
                pageview.setText("0"+String.valueOf(page));
            else pageview.setText(String.valueOf(page));

            return false;


        }
    });



    btnext.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (page<60)
                page++;
            else
                page=1;
            setacursor(page);
            escrevebotao();
            if (page<10)
                pageview.setText("0"+String.valueOf(page));
            else pageview.setText(String.valueOf(page));
        }

    });
    botaopage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            if (p != null)
                showPopup(MainActivity.this, p);
                page = Integer.valueOf(valor);
                if (page>60)
                    page=60;
                if (page<0)
                    page=0;
                if (page<10)
                    pageview.setText("0"+String.valueOf(page));
                else pageview.setText(String.valueOf(page));
        }


    });




}


public void setacursor(Integer pagina){
    cursor.moveToPosition(pagina-1);

}


private void escrevebotao() {
    btdescricao.setText(retornadado("bt"));
    bt01.setTextOff("01 \n" + retornadado("bt01"));
    bt01.setText("01 \n" + retornadado("bt01"));
    bt01.setTextOn("01\n" + retornadado("bt01"));
    bt02.setTextOff("02 \n" + retornadado("bt02"));
    bt02.setText("02 \n" + retornadado("bt02"));
    bt02.setTextOn("02 \n" + retornadado("bt02"));
    bt03.setTextOff("03 \n" + retornadado("bt03"));
    bt03.setText("03 \n" + retornadado("bt03"));
    bt03.setTextOn("03 \n" + retornadado("bt03"));
    bt04.setTextOff("04 \n" + retornadado("bt04"));
    bt04.setText("04 \n" + retornadado("bt04"));
    bt04.setTextOn("04 \n" + retornadado("bt04"));
    bt05.setTextOff("05 \n" + retornadado("bt05"));
    bt05.setText("05 \n" + retornadado("bt05"));
    bt05.setTextOn("05 \n" + retornadado("bt05"));
    bt06.setTextOff("06 \n" + retornadado("bt06"));
    bt06.setText("06 \n" + retornadado("bt06"));
    bt06.setTextOn("06 \n" + retornadado("bt06"));
    bt07.setTextOff("07 \n" + retornadado("bt07"));
    bt07.setText("07 \n" + retornadado("bt07"));
    bt07.setTextOn("07 \n" + retornadado("bt07"));
    bt08.setTextOff("08 \n" + retornadado("bt08"));
    bt08.setText("08 \n" + retornadado("bt08"));
    bt08.setTextOn("08 \n" + retornadado("bt08"));
    bt09.setTextOff("09 \n" + retornadado("bt09"));
    bt09.setText("09 \n" + retornadado("bt09"));
    bt09.setTextOn("09 \n" + retornadado("bt09"));
    bt10.setTextOff("10 \n" + retornadado("bt10"));
    bt10.setText("10 \n" + retornadado("bt10"));
    bt10.setTextOn("10 \n" + retornadado("bt10"));
    bt11.setTextOff("11 \n" + retornadado("bt11"));
    bt11.setText("11 \n" + retornadado("bt11"));
    bt11.setTextOn("11 \n" + retornadado("bt11"));
    bt12.setTextOff("12 \n" + retornadado("bt12"));
    bt12.setText("12 \n" + retornadado("bt12"));
    bt12.setTextOn("12 \n" + retornadado("bt12"));
    bt13.setTextOff("13 \n" + retornadado("bt13"));
    bt13.setText("13 \n" + retornadado("bt13"));
    bt13.setTextOn("13 \n" + retornadado("bt13"));
    bt14.setTextOff("14 \n" + retornadado("bt14"));
    bt14.setText("14 \n" + retornadado("bt14"));
    bt14.setTextOn("14 \n" + retornadado("bt14"));
    bt15.setTextOff("15 \n" + retornadado("bt15"));
    bt15.setText("15 \n" + retornadado("bt15"));
    bt15.setTextOn("15 \n" + retornadado("bt15"));


}


public void nextdado() {
    try{
        cursor.moveToNext();

    }
    catch(Exception erro){
        Exibirmensagem("ERRO!","Erro ao avançar com o " +
                "crusor:  "+ erro.getMessage() , "OK");

    }

}


private void iniciaobjetos() {
    try{
        bt01 = (ToggleButton) findViewById(R.id.btt1);
        bt02 = (ToggleButton) findViewById(R.id.btt2);
        bt03 = (ToggleButton) findViewById(R.id.btt3);
        bt04 = (ToggleButton) findViewById(R.id.btt4);
        bt05 = (ToggleButton) findViewById(R.id.btt5);
        bt06 = (ToggleButton) findViewById(R.id.btt6);
        bt07 = (ToggleButton) findViewById(R.id.btt7);
        bt08 = (ToggleButton) findViewById(R.id.btt8);
        bt09 = (ToggleButton) findViewById(R.id.btt9);
        bt10 = (ToggleButton) findViewById(R.id.btt10);
        bt11 = (ToggleButton) findViewById(R.id.btt11);
        bt12 = (ToggleButton) findViewById(R.id.btt12);
        bt13 = (ToggleButton) findViewById(R.id.btt13);
        bt14 = (ToggleButton) findViewById(R.id.btt14);
        bt15 = (ToggleButton) findViewById(R.id.btt15);
        btnext = (Button) findViewById(R.id.btNext);
        btprevious = (Button) findViewById(R.id.BtPrevious);
        pageview = (Button) findViewById(R.id.tv2);
        btdescricao = (Button) findViewById(R.id.btdescricao);
        botaopage = (Button)findViewById(R.id.tv2);
        etpage = (EditText)findViewById(R.id.etpage);

    }
    catch(Exception erro){
        Exibirmensagem("ERRO", "erro ao iniciar objetos" +erro.getMessage(),"OK");
    }

}


public void abrebanco(){
    try{
        Banco = openOrCreateDatabase("banco", MODE_WORLD_WRITEABLE, null);
        String sql = "CREATE TABLE IF NOT EXISTS pages (ID INTEGER PRIMARY KEY AUTOINCREMENT" +
                ", bt TEXT,bt01 TEXT, bt02 TEXT, bt03 TEXT, bt04 TEXT, bt05 TEXT, bt06 TEXT, " +
                "bt07 TEXT, bt08 TEXT, bt09 TEXT, bt10 TEXT, bt11 TEXT, bt12 TEXT, " +
                "bt13 TEXT, bt14 TEXT, bt15 TEXT)";
        Banco.execSQL(sql);

    }
    catch(Exception erro){
        Exibirmensagem("BANCO", "erro ao criar banco: =/"+ erro.getMessage(), "ok");
    }
}
public void fechabanco(){
    try{
        Banco.close();          
    }
    catch(Exception erro){
        Exibirmensagem("BANCO", "erro ao fechar banco: "+ erro.getMessage(), "ok");
    }
}

public boolean buscardados(){
    try{
        cursor = Banco.query("pages",
                new String [] {"bt","bt01","bt02","bt03","bt04","bt05",
                "bt06","bt07","bt08","bt09","bt10",
                "bt11","bt12","bt13","bt14","bt15"}
        , null, null, null, null, null);

        if (cursor.getCount() != 0){
            cursor.moveToFirst();

        }

        return true;
    }
    catch(Exception erro){
        Exibirmensagem("BANCO", "erro ao buscar no banco: "+ erro.getMessage(), "ok");
        return false;
    }
}





public String retornadado(String x){
    String dado = cursor.getString(cursor.getColumnIndex(x));
    return dado;
}


public void Exibirmensagem (String titulo, 
        String texto, String button)
{
    AlertDialog.Builder mensagem = 
            new AlertDialog.Builder(MainActivity.this);
    mensagem.setTitle(titulo);
    mensagem.setMessage(texto);
    mensagem.setNeutralButton(button,null);
    mensagem.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();
    if (id == R.id.menueditar) {
        Intent iedicao = new Intent(MainActivity.this, paginadeedicao.class);


        iedicao.putExtra("pagina", page);
        startActivity(iedicao);
        MainActivity.this.finish();
        //startActivity(new Intent(MainActivity.this, edicao.class));
        return true;
    }
    if (id == R.id.menusair) {
        AlertDialog.Builder mensagem = 
                new AlertDialog.Builder(MainActivity.this);
        mensagem.setTitle("Atenção?");
        mensagem.setMessage("Deseja realmente sair?");

        mensagem.setPositiveButton("Sim",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                MainActivity.this.finish();

            }});
        mensagem.setNegativeButton("Não",null);
        mensagem.show();
        return true;

    }
    if (id == R.id.menusobre) {
        Exibirmensagem("Playback View", "   Esse aplicativo foi desenvolvido com " +
                "objetivo de criar um roller digital e ecologicamente correto, para os " +
                "iluminadores utilizarem como guia dos seus playbacks. \n"+
                "   Todo o copyright ou direitos de propriedade intelectual " +
                "presente em Textos, Imagens, Sons, Softwares e outros conteúdos " +
                "do aplicativo, são de propriedade de Star Lighting Division! \n"+
                "   Em caso de sugestões, " +
                "dúvidas e outros, entre em contato conosco: " +
                "\n(19) 3838-8320 ou [email protected].", "OK");
        return true;
    }
    if (id == R.id.menusite) {
        Uri uriPaginaWeb = Uri.parse("http://star.ind.br/");
        Intent IPaginaWeb = new Intent(Intent.ACTION_VIEW,uriPaginaWeb);
        startActivity(IPaginaWeb);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
    super.onStart();
    Log.i("main", "Executou o onStart();");
}
@Override
protected void onResume() {
    super.onResume();
    Log.i("main", "Executou o onResume();");
}
@Override
protected void onPause() {
    super.onPause();
    Log.i("main", "Executou o onPause();");
}
@Override
protected void onStop() {
    super.onStop();
    Log.i("main", "Executou o onStop();");
}
@Override
protected void onDestroy() {
    super.onDestroy();
    //banco.close();
    Log.i("main", "Executou o onDestry();");
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    int[] location = new int[2];
    Button button = (Button) findViewById(R.id.tv2);

    button.getLocationOnScreen(location);

    p.x = location[0];
    p.y = location[1];
}

@SuppressWarnings("deprecation")
public void showPopup(final Activity context, Point p) {
    int popupWidth = 300;
    int popupHeight = 400;
    Boolean x=false;
    etpage2 = (EditText) findViewById(R.id.buttonir);
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
    LayoutInflater layoutInflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
    final PopupWindow popup = new PopupWindow(context);
    popup.setContentView(layout);
    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);
    popup.setFocusable(true);

    popup.setBackgroundDrawable(new BitmapDrawable());
    popup.showAtLocation(layout, 
            Gravity.NO_GRAVITY, 1, 1 );
    Button close = (Button) layout.findViewById(R.id.buttonir);
    close.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            popup.dismiss();
            valor = etpage.getText().toString();

        }



    });


}

}

1 answer

1


The method getText() returns a Editable and there is no equivalent parameter for String.valueOf().

To get the string from Edittext, do so:

valor = etpage.getText().toString()
  • Good afternoon, vlw by the tip, but it didn’t work. =/,

  • It can report the error that was happening or that was showing on the console?

  • I put in question. vlw

  • console n appears nothing, and in the app it only stops responding

  • Well, if it is Nullpointerexception is because you are trying to access something that has not been instantiated, so it is null. The only possibility in this line is the field itself by the variable etpage. This was done in some code snippet before the click?

  • excuse ignorance, standardize is when you do the etpage = (Edittext) findbyviewid(r.id.botao)?

  • if yes, I did it in global scope. I’ll put the whole code

  • was looking at the debug part, it is not getting the content of Edit text

  • Then it is because the instance is null. The last hint is to check if in your layout there really is a EditText with the id @+id/etpage.

  • but has =/ this very strange =/ I will redo, who knows the right

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.