2012-03-12 8 views
1

基本的には、これに14個のボタンを追加する予定です。これらのボタンはすべて、2回目のアクティビティでスピナーの変更だけで同じアクティビティに移動します。だから、私は同じアクティビティにつながるすべてのボタンのインテントオブジェクトを作成したくはありません(アプリの速度が遅くなるため)。だから、私はidを取得するためにボタンにタグを割り当て、そのIDを新しいアクティビティに渡そうとしました。しかし、それは動作しません。 AVDは最初のアクティビティのみを表示し、ボタンをクリックしても何も起こりません。ボタンをクリックして別のアクティビティに切り替えることができない

マイメイン画面(Test1を)活性:

public class Test1Activity extends Activity { 


public static final String pass="com.sanjay.test1._clicked"; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
Button button1=(Button) findViewById(R.id.button1); 
Button button2=(Button) findViewById(R.id.button2); 

button1.setId(1); 
button2.setId(2); 

button1.setOnClickListener(new MyOnClickListener()); 
button2.setOnClickListener(new MyOnClickListener()); 

} 
private class MyOnClickListener implements OnClickListener{ 
@Override 
public void onClick(View v) 
{ 
int id=v.getId(); 
Intent i=new Intent(Test1Activity.this,Sanjay.class); 
i.putExtra(pass, id); 

} 

} 
} 

main.xml

<?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="Length" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Weight" /> 

</LinearLayout> 

第二クラス(サンジェイ)活性:

public class Sanjay extends Activity { 

/** Called when the activity is first created. */ 
int a; 
TextView text; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second); 

    text=(TextView) findViewById(R.id.text); 
    a=getIntent().getExtras().getInt(Test1Activity.pass); 

    text.setText(" "+a); 
    // TODO Auto-generated method stub 
} 

} 

Second.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.sanjay.rotation" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".RotationActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 
+0

あなたはインテントを作成しましたが、アクティビティを開始しませんでした。インテントメッセージでアクティビティを開始します。インテントi =新しいインテント(...); startActivity(i); – Jayabal

答えて

1

あなたはいない活動を開始することはありません場合のonClick機能でstartActivity(i)を追加する必要があります。 Androidマニフェストにも追加する必要があります。

このよう

private class MyOnClickListener implements OnClickListener{ 
@Override 
public void onClick(View v) 
{ 
int id=v.getId(); 
Intent i=new Intent(Test1Activity.this,Sanjay.class); 
i.putExtra(pass, id); 
startActivity(i); 

} 

} 

そして、あなたのマニフェスト:マニフェストへのエントリを必要とSanjay

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.sanjay.rotation" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="15" /> 

<application 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" > 
<activity 
    android:name=".RotationActivity" 
    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=".Sanjay" 
    android:label="Sanjay Label"> 
</activity> 
</application> 

</manifest> 
+0

ありがとうルシア!私は始動性を完全に忘れていましたが、それをマニフェストに含める必要があることを知らなかったのです。今すぐWonderfully Working:D @Lucia – Exorcist

1

あなたの活動。

<activity 
     android:name=".Sanjay" 
     android:label="@string/app_name" > 
</activity> 
0

任意のデータを格納するためにsetId()を使用しないでください。これは、ビューが参照を取得するためにシステムによって使用されるIDです。

Viewには、このような任意のデータを格納するためのsetTag()メソッドがあります。あなたはIdの代わりにTagを使うべきです。

button1.setTag(1); 
button2.setTag(2); 

してから、この方法からそれらを引き戻す:このよう

int id= (Integer)v.getTag(); 
+0

'int id =(Integer)v.setTag(1);'これは第1ボタンに値 '1'を割り当てますか? – Exorcist

+0

いいえ、getTag()は設定されていません。パラメータを渡す必要はありません。しかし、あなたはそれを{g} etTag()にする必要があります – FoamyGuy

+0

アドバイスをいただきありがとうございます。私はそれを試してみる。とても有難い。 – Exorcist

0

あなたは、Androidマニフェストで2番目の活動を宣言していませんでした。あなたの<application>タグ内にこれを追加します。アドバイスとして

<activity android:name=".Sanjay"> 
</activity> 

はあなたの問題を伝える警告メッセージを発見したであろうこのような場合には、LogCatに常に目を離しません。

+0

Logcatの使い方をお勧めしますか?@YuviDroid – Exorcist

+0

Eclipseでは、 'Window'、 'Open Perspective'、 'DDMS'(「Other ..」を選択していない場合)を選択します。次に、下部にLogCatビューが表示されます。次に、Devicesビューから接続したデバイスを選択すると、LogCatのアップデートが開始され、すべてのLogメッセージが表示されます。 – YuviDroid

+0

私はLogCatの起動を意味しませんでした。私はそれから情報を抽出することを意味しました。私はAndroidから始まったばかりです。期待しています。 – Exorcist

関連する問題