setcontent view not working

2019-05-23 07:59发布

button.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){
                setContentView(R.layout.activity_chart);
            }

        });

Hi i have the above code wherein upon clicking a button i am trying to display them activity activity_chart. In that activity i want to display a graph. Here i am calling a method createIntent(). But my problem is that the graph is not getting plotted. Please help i am new to android.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        createIntent();

    }

    public Intent createIntent() 

     {

...

}

Am i calling the method right.

1条回答
地球回转人心会变
2楼-- · 2019-05-23 08:20

A new Activity is called with:

startActivity(new Intent(currentActivity.this, nextActivity.class));

Then in your new Activities onCreate(Bundle savedInstance) method you can call setContentView(Layout layout); to set the new Layout.

So if you want to change the Activity when clicking on a Button you have to do the following:

button.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){
                startActivity(new Intent(currentActivity.this, nextActivity.class));
            }

        });

You are currently only changing the layout of the current Activity when clicking the button and not changing to another Activity.

I hope I understood you correctly. If not then provide me with some more code so I can try to understand what you want to do.

查看更多
登录 后发表回答