简体   繁体   中英

Issue created a button in layout (Android)?

I am trying to use two buttons in one layout:

  • a button to notes, which is working below
  • a button to calculator (button10)

I get this error on super.oncreate:

"The method onCreate(Bundle) is undefined for the type Object" 

main.java:

public class IzzynActivity extends Activity{

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button wg = (Button) findViewById(R.id.button1);
        wg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(IzzynActivity.this, notes.class);
                IzzynActivity.this.startActivity(myIntent);
            }

            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                Button wg = (Button) findViewById(R.id.button10);
                wg.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent intent = new Intent(IzzynActivity.this, calculator.class);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
        });

            }


}
    }
}

I don't know what you've done but you've messed up your code big time. My assumption is that you've been copying a tutorial without reading what's actually going on and therefore not really understanding what you're doing.

Here is what your code should look like (untested, I've just typed this up now, but this is the jist).

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // find the first button and set an on click listener
        Button wg = (Button) findViewById(R.id.button1);
        wg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(IzzynActivity.this, notes.class);
                IzzynActivity.this.startActivity(myIntent);
            }
        });

        // find the next button and set an on click listener
        Button otherButton = (Button)findViewById(R.id.button10);
        otherButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                Intent intent = new Intent(IzzynActivity.this, calculator.class);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

}

I don't know exactly what you did in that class but if you want two buttons in your layout then put them in your layout and search for them in the activity's onCreate method:

R.layout.main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
     <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
      <Button
        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button10" />
</LinearLayout>

and then in your activity:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button wg1 = (Button) findViewById(R.id.button1);
        wg1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(IzzynActivity.this, notes.class);
                IzzynActivity.this.startActivity(myIntent);
            }
        });
        Button wg10 = (Button) findViewById(R.id.button10);
                wg10.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent intent = new Intent(IzzynActivity.this, calculator.class);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM