2012-03-04 31 views
0

我想在一个布局中使用两个按钮:问题在布局(Android)中创建了一个按钮?

  • 一键票据,这是工作下面
  • 一个按钮,计算器(button10)

我得到的超极本的错误.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(); 
        } 
     }); 

      } 


} 
    } 
} 
+0

这里有什么问题? – 2012-03-04 17:57:06

+1

每个活动只允许有一个onCreate方法。下面的两个答案将你的问题全部整理出来。也许你应该阅读一些HelloAndroid教程,以了解你在做什么。 – 2012-03-04 17:58:46

回答

0

我不知道你在那类做了什么,但如果你想在你的布局两个按钮,然后把它们放在你的布局,并在活动的onCreate方法寻找他们:

[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> 

,然后在你的活动:

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(); 
        } 
     }); 
+0

该按钮有效。但是,不要把我带到计算器类,它将我带到主屏幕...... – user1248404 2012-03-04 18:05:44

+0

@ user1248404当您使用'startActivityForResult()'从另一个活动开始此活动时使用'setResult',并在活动结束时预期结果。如果所有你想要的是按钮点击启动'计算器'类,那么简单地使用你在第一个点击监听器中使用的:'Intent myIntent = new Intent(IzzynActivity.this,calculator.class); IzzynActivity.this.startActivity(myIntent);'。在关闭当前活动时使用'finish()'方法。 – Luksprog 2012-03-04 18:10:23

+0

非常感谢谢谢! – user1248404 2012-03-04 18:29:18

1

我不知道你做了什么,但你已经搞砸了你的代码大的时间。我的假设是,你一直在复制一个教程而没有阅读实际正在发生的事情,因此不能真正理解你在做什么。

这里是你的代码应该看起来像(未经测试,我现在只是输入这个,但这是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(); 
      } 
     }); 

} 
+0

仔细阅读本文并弄清楚发生了什么,并与您比较,看看您的错误是什么。老实说,它会帮助你学习。 :)# – 2012-03-04 17:58:13

+0

该按钮正常工作!但它让我回到了Android主屏幕。这是为什么? – user1248404 2012-03-04 18:12:57

+0

摆脱行'finish()'。这将关闭您所进行的活动,并将您发送回最后一个活动(我认为这是您的Android主屏幕)。然后你需要确保你调用'startActivity(intent)',你曾经有'finish()'。这将启动您在上面的行中创建的活动。 – 2012-03-04 18:21:30