1

こんにちは、すべての回答に事前に感謝します。

私が抱えている問題は、「Calander.class」と呼ばれる 2 番目のアクティビティが、メインのアクティビティ呼び出し「ShiftSelection」から呼び出されていないことです。私は一般的にコーディングとアンドロイドに不慣れで、基本を学ぼうとしていますが、この問題は本当に私を困惑させました. アクティビティをマニフェスト ファイルに追加しましたが、インテントを正しく呼び出していると思いますが、ボタンのアプリケーション onClick を実行すると、2 番目のアクティビティを呼び出す必要がありますが、何もしません。

私は何を間違っていますか?これが単純な答えである場合は非常に申し訳ありませんが、私はさまざまなことを何時間も試みて失敗したことを保証します.

最初のアクティビティとマニフェスト ファイルのコードは次のとおりです。

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);
             }

          });

    }

}

そしてマニフェストファイル

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

助けてくれてありがとう。

4

5 に答える 5

3

あなたの意図では、代わりにgetApplicationContext()これを使用してみてください

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

多分これはあなたを助ける

于 2012-06-07T21:43:41.653 に答える
1

ボタンを正しく取得していません。そうすることで、表示されないボタンに onClick リスナーを割り当てます。

する代わりに

Button openButton = new Button(this);

次のようにして、既存のレイアウトでボタンを見つけようとする必要があります

Button openButton = (Button)findViewById(R.id.openButton);  

レイアウト ファイルR.id.openButtonでボタンに割り当てた ID に置き換えますR.layout.shiftselection

現在行っている方法では、新しいボタンを作成し、それをレイアウトに添付しないため、表示されません

于 2012-06-07T21:24:57.717 に答える
0

これを試して

Intent intent = new Intent(ShiftSelection.this, Calander.class);
于 2012-06-08T06:23:08.950 に答える
0

役立つかもしれない別のアイデア: ボタン ビューの onClick 属性を使用します。これは、アプリのスコープ内の任意のメソッドの名前に設定できますが、ビューに関連付けられたアクティビティでメソッドを使用するのが最も一般的です。メソッドのシグネチャは

public void methodname (View v)

于 2012-06-07T23:53:09.727 に答える
0

アクティビティに 2 つ、マニフェストに 1 つ必要な 3 つの変更があります


ShiftSelection.javaアクティビティでこのコードを置き換えます

Button openButton = new Button(this);

Button openButton = (Button)findViewById(R.id.button1);

この行を変更します(同じアクティビティの onClick メソッド)

Intent intent = new Intent(getApplicationContext(), Calander.class);

Intent intent = new Intent(ShiftSelection.this, Calander.class);

次に、shiftselection.xmlに次のようなボタンが 1 つ必要です。

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

最後に、 AndroidManifest.xmlファイルで両方のアクティビティのエントリを次のように作成することが最も重要です。

<?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">
    <uses-sdk android:minSdkVersion="8" />
    <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"
                  android:label="@string/app_name">
            <intent-filter>                
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

それでも問題が解決しない場合はお知らせください。

于 2012-06-08T19:05:01.567 に答える