В принципе, я собираюсь включить еще 14 кнопок в это, и все они будут работать в одном и том же режиме, только с изменением числа оборотов во втором действии. Поэтому я не хочу создавать объект Intent для каждой кнопки, чтобы привести к такой же активности (поскольку это замедляет приложение). Поэтому я назначил метки кнопке, чтобы получить идентификатор и попытался передать этот идентификатор новому действию. Но это не работает. 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>
Вы создали намерение, но не начали свою деятельность. поэтому начните работу с сообщения о намерениях. Intent i = new Intent (...); startActivity (я); – Jayabal