android - 无法调用第二个 Activity

标签 android

大家好,提前感谢您的回答。

我遇到的问题是我的第二个 Activity “Calander.class”没有从我的主要 Activity “ShiftSelection”中调用。一般来说,我是编码和 android 的新手,我只是想学习基础知识,但这个问题真的让我很困惑。 我已将 Activity 添加到 Manifest 文件中,并认为我正确地调用了 Intents,但是当我的 Button 运行应调用第二个 Activity 的应用程序 onClick 时,它没有做任何事情。

我做错了什么?非常抱歉,如果这是一个简单的答案,但我向你保证,我已经尝试了很多小时尝试不同的事情,但都失败了。

我的第一个 Activity 和 list 文件的代码如下。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

 public class ShiftSelection extends Activity{

    Button openButton;
    RadioGroup shiftSelection; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shiftselection);    

    shiftSelection = new RadioGroup(this);

    RadioButton shiftPattern1 = new RadioButton(this); //shiftPattern1 = 4 shift 
    shiftPattern1.setText("4 shift");
    shiftPattern1.setChecked(true);
    shiftPattern1.setId(01);

    RadioButton shiftPattern2 = new RadioButton(this); //shiftPattern2 = 6     shift        
    shiftPattern2.setText("4 shift");
    shiftPattern2.setId(02);        

    shiftSelection.addView(shiftPattern1);
    shiftSelection.addView(shiftPattern2);        

    Button openButton = new Button(this);
    openButton.setText("open");
    openButton.setOnClickListener(new OnClickListener() {

          @Override
           public void onClick(View v) {
              Intent intent = new Intent(getApplicationContext(), Calander.class);
              int genderID = shiftSelection.getCheckedRadioButtonId();

            switch(genderID){

            case 01:
             intent.putExtra("shiftPattern", "1");
             break;

            case 02:
             intent.putExtra("shiftPattern", "2");
             break;
             }
             startActivity(intent);
             }

          });

    }

}

和 list 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.examples"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".ShiftSelection"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
        <activity android:name=".Calander">
        </activity>              
</application>
</manifest> 

感谢您的帮助。

最佳答案

在你的 Intent 中,而不是 getApplicationContext(),尝试使用这个

Intent intent = new Intent (this, Calendar.class);

也许这对你有帮助

关于android - 无法调用第二个 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10940085/

相关文章:

android - 使用用户输入从android sqlite检索数据

android - whatsapp 超链接 - 结合 whatsapp ://send? 与 intent://send/

android - Navigation Drawer + Sliding Tabs inside fragment Android

Android "Support Screens"不工作?

android - 如何在 Canvas 上绘制大量矩形并且性能良好?

java - Android 方法 openFileOutput 出现 "open failed: ENOENT"错误

java - 在 Android 中解析 JSON 时出现 OutOfMemoryError

php - SHA1 校验和在 php 和 android 中对于同一个文件变得不同

Android-更改 NumberPicker 步数或增量

android - Firebase 存储最佳实践