Home » Android » Trying to make listview visible after button is clicked

Trying to make listview visible after button is clicked

Posted by: admin November 1, 2017 Leave a comment

Questions:

The application works but when i added the buttonclick the application gives no errors but on emulator it just crashes without starting.

This is the expected result:
https://imgur.com/a/QyKG1

Here is the entire file:
https://pastebin.com/e6Q5ViuS

Here is the code that contains the error:

public class MainActivity extends AppCompatActivity {

    ArrayList<Product> arrayList;
    ListView lv;
    EditText txtUrl;
    Button btnSubmit;
    String EditTextValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arrayList = new ArrayList<>();
        lv = (ListView) findViewById(R.id.listView);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EditTextValue = txtUrl.getText().toString();


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new ReadJSON().execute(EditTextValue);
                        }
                    });

            }
        });


    }
Answers:

You need to initialize this way :

btnSubmit = (Button) findViewById(R.id.[your id]);

BEFORE you use it to set the onClick listener…

Questions:
Answers:

You have not initialized button before adding click listener
That’s why your app crashes