0

Android Code The App is not opening, whenever i run the app the error occurred unfortunately the tables for children has stopped. The app installed in AVD Manager properly. But not running.

public class MainActivity extends AppCompatActivity
{
    EditText ed;
    Button b;
    ListView li;
    int table[]= new int[11];
    ListAdapter arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click();
    }
    public void click() {
        ed = (EditText) findViewById(R.id.editText);
        b = (Button) findViewById(R.id.button);
        li = (ListView) findViewById(R.id.listView);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                int number = Integer.parseInt(ed.getText().toString());
                if (number > 0)
                {
                    for (int a = 1; a <= 12; a++)
                    {
                        table[a] = a * number;
                    }
                }
            }

        });
        arrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_list_item_1, table['a']);
        li.setAdapter(arrayAdapter);
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

1

I tried your code and made a few changes for it to work..

 public class MainActivity extends AppCompatActivity
{
EditText ed;
Button b;
ListView li;
ArrayList table =new ArrayList();
ListAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    click();
}
public void click() {
    ed = (EditText) findViewById(R.id.editText);
    b = (Button) findViewById(R.id.button);
    li = (ListView) findViewById(R.id.listView);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                int number = Integer.parseInt(ed.getText().toString());
                if (number > 0) {
                    for (int a = 0; a < 11; a++) {
                        table.add(a * number);
                    }
                }

                arrayAdapter = new ArrayAdapter<Integer>(view.getContext(), android.R.layout.simple_list_item_1, table);
                li.setAdapter(arrayAdapter);


            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }

    });

  }
}

I moved the adapter into the click() and used ArrayList instead of integer Array...Try this....

hope this helps.. :)

Benedict
  • 458
  • 2
  • 13
0

You probably have an indexOutOfBounds Error because your array has a size of 11 but you iterate to a size of 13.

Change your

int table[]= new int[11];

to

int table[]= new int[13];

also change

arrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_list_item_1, table['a']);

to

arrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_list_item_1, table);
babadaba
  • 814
  • 5
  • 20
  • Look at your logcat output. There should be an error message in your Android Studio. Please edit this log in your question above – babadaba Aug 08 '16 at 08:04
  • logcat-Session 'app': Error Installing restart patchesSession 'app': Error Installing restart patches – Mahad Maqsood Aug 08 '16 at 08:09
  • Try deinstalling your app manually before building the application – babadaba Aug 08 '16 at 08:11
  • But when i change table['a'] to table , error appeared ---Error:(48, 24) error: no suitable constructor found for ArrayAdapter(MainActivity,int,int[]) constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable (argument mismatch; int[] cannot be converted to int) constructor ArrayAdapter.ArrayAdapter(Context,int,Integer[]) is not applicable (argument mismatch; int[] cannot be converted to Integer[]) constructor ArrayAdapter.ArrayAdapter(Context,int,List) is not applicable (argument mismatch; int[] cannot be converted to List) – Mahad Maqsood Aug 08 '16 at 08:12
  • Then change your int table[]= new int[11]; to Integer table[]= new Integer[11]; – babadaba Aug 08 '16 at 08:22
  • still giving error the unfortunately the app has stopped. – Mahad Maqsood Aug 08 '16 at 08:50