1

I am building an app which after you input some parameter(P1,P2,P3) and click save, will pop an AlertDialog requesting a number, after the user key in this number, the program will input the information (P1,P2,P3) in a SQLite register table T1. And it will create as many entries as the user define in the AlertDialog in a SQLite T2.

Basically I have the next code:

 switch(item.getItemId()){
        case R.id.Save:

            final String UpdateStudyCode = eStudyCode.getText().toString();
            final String UpdateStudyDescription = eStudyDescription.getText().toString();
            final String UpdateStudyAnalyst = eStudyAnalyst.getText().toString();

            if( l == 0){
            //New Register Study
                if (UpdateStudyCode == ""){
                    Toast.makeText(getApplicationContext(), "Empty Study Code", Toast.LENGTH_SHORT).show();
                }
                else{                       
                    AlertDialog.Builder dialogCreateElements = new AlertDialog.Builder(this);

                    dialogCreateElements.setIcon(android.R.drawable.ic_dialog_alert);
                    dialogCreateElements.setTitle(getResources().getString(R.string.element_create_title));
                    dialogCreateElements.setMessage(getResources().getString(R.string.element_create_messaje));
                    final EditText inputelementstarget = new EditText(this);
                    //dialogCreateElements.setInputType(InputType.TYPE_CLASS_PHONE);
                    //dialogCreateElements.setRawInputType(Configuration.KEYBOARD_12KEY);
                    dialogCreateElements.setView(inputelementstarget);
                    dialogCreateElements.setCancelable(false);
                    dialogCreateElements.setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {   
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // TODO Auto-generated method stub
                            database db = new database (studydetail.this);
                            db.open();
                            //Create Study
                            long studyid;
                            studyid = db.createEntryStudy(UpdateStudyCode,UpdateStudyDescription,UpdateStudyAnalyst);
                            //Create number of specific elements 
                            int element=1;
                            String value = inputelementstarget.getText().toString();
                            int target = Integer.parseInt(value);
                            do
                            {
                                Toast.makeText(getApplicationContext(), element, Toast.LENGTH_SHORT).show();
                                db.createEntryElements(element, studyid);
                                element++;
                            }
                            while (element < target+1);

                            db.close();

                            Toast.makeText(studydetail.this, R.string.element_create_confirmation, Toast.LENGTH_SHORT).show();
                            setResult(RESULT_OK);

                        }
                    });

                  dialogCreateElements.setNegativeButton(android.R.string.no, null);
                  dialogCreateElements.show();

Currently I receive the next error and only the entry in T1 is created FATAL EXCEPTION:

 main
 Process: com.example.standardtime, PID: 1873
      android.content.res.Resources$NotFoundException: String resource ID #0x1
      at android.content.res.Resources.getText(Resources.java:274)
      at android.widget.Toast.makeText(Toast.java:277)
      at com.example.standardtime.studydetail$1.onClick(studydetail.java:121)
      at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5221)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
Juan Sala
  • 15
  • 5

3 Answers3

0

To give you info, the error is at this line,

 String value = inputelementstarget.getText().toString();

I didn't find where you actually initialized this inputelementstarget. Actually this is not an initialization you needed

final EditText inputelementstarget = new EditText(this);

And I think you are trying to add EditText in AlertDialog.

For that purpose what you have to do is

  • Create a layout with EditText in it.
  • Inflate that like this

    LayoutInflater li = LayoutInflater.from(context); View promptsView = li.inflate(R.layout.prompts, null);

  • Then After you have to initialize that

    final EditText userInput = (EditText) promptsView .findViewById(R.id.editTextDialogUserInput);

For A Complete reference have a look here. See this answer. You will get a lot of information.

Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
0

You need to do this way:

  1. Toast message must be in String form.

Toast.makeText(getApplicationContext(), element, Toast.LENGTH_SHORT).show();

element must be String, currently it is Integer.

  1. Toast.makeText(studydetail.this, R.string.element_create_confirmation, Toast.LENGTH_SHORT).show();

to

Toast.makeText(studydetail.this, getString(R.string.element_create_confirmation), Toast.LENGTH_SHORT).show();

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
0

The error is occurring when you want to show element value in:

Toast.makeText(getApplicationContext(), element, Toast.LENGTH_SHORT).show();

If you want to show element variable value, just use String.valueOf(element) instead, like this:

Toast.makeText(getApplicationContext(), String.valueOf(element) , Toast.LENGTH_SHORT).show();

And you'll get the desired result.

Hope this helps!!

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57