java - 无法在 Android 中启动其他 Activity

标签 java android eclipse

我正在学习android developpement,我写了一个简短而简单的代码,但它不起作用。 我无法开始另一项 Activity ,尽管尝试了很多次!这是主要 Activity 的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pageaccueil);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.pageaccueil, menu);
    return true;
}
public void onCreate1(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_pageaccueil);

  final Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
    Intent intent = new Intent(Pageaccueil.this, Devise.class);
    startActivity(intent);
    }
});
   }
}

以及来自第一个/主要 Activity 的 XML 布局的按钮部分:

    <Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="38dp"
    android:text="convertisseur de devises"
  />

第二个 Activity 是“设计”,这是它的代码: 导入 android.os.Bundle; 导入 android.app.Activity; 导入 android.view.Menu;

 public class Devise extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_devise);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.devise, menu);
    return true;
}

}

有谁知道我怎样才能启动第二个 Activity ?我试了很多次都没有成功。

提前致谢!!

最佳答案

在您的 onCreate() 中使用 Following 而不是 onCreate1()

final Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
    Intent intent = new Intent(Pageaccueil.this, Devise.class);
    startActivity(intent);
    }
});
   }

同时研究 Activity 的生命周期 http://developer.android.com/training/basics/activity-lifecycle/index.html

关于java - 无法在 Android 中启动其他 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18109795/

相关文章:

android - 如何自定义处理for Fragment的BackPressed键?

java - 如何正确使用枚举进行简单的价格计算 (Java)

android - 我怎么知道用户是否截取了我的应用程序的屏幕截图?

android - Android PackageManager 是否会接受声明 systemOrSignature 权限的应用程序?

eclipse - Notepad++ 类型的 FTP 插件,用于 sublime text、netbeans 或 eclipse

eclipse - 导入/导出项目首选项

java - 使用 Maven 运行 GWTTestCase

Java 线程亲和性

java - 该类型未定义方法 "X"

java - 在eclipse上安装逆时针插件失败