2012-10-02 64 views
1

对不起,我是非常新的android开发,现在我想锻炼,当用户按下按钮,它将启动Actitity2,并且同样当用户按下Activity2中的取消按钮,它将回到原来的活动。Android:意图在一个活动中发起另一个活动

我已经引用到本书编写应用程序,但它似乎不能工作,编码是看起来简单,如下:

public class NameIndex extends Activity 
{ 
     // called when the activity is first created 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.name_index); 

      public button_cancel_click (View view) { /////// <-- ERROR AT THIS LINE 
       Intent intent = new Intent (this, GameIndex.class); 
       startActivity(intent); 
      } 
     } // end method onCreate 
} 

XML结构如下:

<TableRow android:id="@+id/tableRow1" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp"  
     android:layout_span="2" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_span="1" 
      android:layout_weight="1" 
      android:onClick="button_cancel_click"    
      android:text="Cancel" />  
    </TableRow>   

Eclipse的将上述错误行报告为“button_cancel_click无法解析为类型”,而对于

查看“用于参数视图的非法修饰符;只有最终被允许

这怎么可能得到解决

+2

您button_cancel_click里面的onCreate外移动,一个方法不可能在另外一个 – njzk2

+0

非常感谢。 !我明白了你的观点,并将它移到了外面,并且它可以工抱歉这么简单的问题! – pearmak

回答

4

你正在编写中的OnCreate函数体button_cancel_click函数的定义更正是这样的:

public void onCreate(Bundle savedInstanceState) 
{ 
. 
. 
. 
} 
public button_cancel_click (View view) 
{ 
. 
. 
} 
+0

非常感谢。有用! – pearmak

0

的听众应该这样添加:?

super.onCreate(savedInstanceState); 
setContentView(R.layout.name_index); 
Button button = (Button) findViewById(R.id.button1); 
button.setOnClickListener(new OnClickListener() {      
        public void onClick(View v) { 
         Intent intent = new Intent (this, GameIndex.class); 
      startActivity(intent); 
        } 
       }); 
+0

不同意,您也可以通过xml调用点击操作,'android:onClick'是给定的属性。 –

相关问题