A-P
A-P

Reputation: 41

Android Button malfunction

Im trying to get my two buttons to work. The first button is supposed to display my first webview activity and the second button is to show the second webview activity. Ive been playign around with my intents and so far ive come to the conclusion that it must have something to do with it because i have my second button displaying the first webview, but my first button doesnt do anything. Any suggestions?

import android.widget.Button;

public class MainActivity extends Activity {

    private Button button;

    public void onCreate(Bundle savedInstanceState) {       final Context context = this;

        super.onCreate(savedInstanceState);         setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonUrl);


        button.setOnClickListener(new OnClickListener() {

            @Override           public void onClick(View arg0) {

                Intent intent = new Intent(context, WebViewActivity.class);
                startActivity(intent);

            }

        });







        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                button = (Button) findViewById(R.id.buttonurl2);

                button.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        Intent intent = new Intent(context, WebViewActivity2.class);
                        startActivity(intent);







                   }});



    } }

Upvotes: 0

Views: 63

Answers (2)

user1707035
user1707035

Reputation:

In addition to above point by Jlewis071, i would recommend you to use two reference variable for two button.

Since you are making only one reference to hold button, so only last made reference is hold, and any previous reference would be discarded.

You should make

Button button1 = <your button1>;
Button button2 = <your button2>;

Upvotes: 0

Jlewis071
Jlewis071

Reputation: 175

Why do you have the lines:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

twice? That could very well be the issue, since you reset the content view and thus lose the on click change you made before the second call of

setContentView(R.layout.main);

In short, remove the second appearance of the above two lines to fix the problem.

Upvotes: 1

Related Questions