Android 使用按钮开始新的 activity
Android Using a button to start new activity
如何使用 Android 中的按钮开始新的 activity?
这是我的应用程序中按钮 "A" 的代码:
final TextView parkAvaliableA = (TextView) findViewById(R.id.parkASlot);
parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
Button buttonA = (Button) findViewById(R.id.parkA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(SlotA.available_slot == 0)
{
Toast.makeText(getApplicationContext(),
"Parking slot A is full",
Toast.LENGTH_SHORT).show();
}
else
{
SlotA.available_slot -= 1;
db.updateSlot(new ParkingSlot());
}
parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
Intent myIntent = new Intent(MainMap.this, ParkA.class);
MainMap.this.startActivity(myIntent);
}
});
当我按下按钮时,应用程序崩溃了。 MainMap.this.startActivity(myIntent);
错了吗?
更新:
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.km.parkit"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".MainMap"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/com.km.parkit" />
</intent-filter>
</activity>
<activity
android:name="com.km.parkit.parka"
android:label="@string/app_name" />
<activity
android:name="com.km.parkit.parkb"
android:label="@string/app_name" />
<activity
android:name="com.km.parkit.parkc"
android:label="@string/app_name" />
<activity
android:name="com.km.parkit.parkd"
android:label="@string/app_name" />
<!-- Google Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBf-0hUDrDTK-NOveZrnT8wec5TTTLqjAw" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
我已经在应用程序中声明了另一个 类,是否缺少某些内容?
将清单中的 Activity 名称更改为
<activity
android:name="com.km.parkit.ParkA"
android:label="@string/app_name" />
而不是com.km.parkit.parka
区分大小写。
如何使用 Android 中的按钮开始新的 activity?
这是我的应用程序中按钮 "A" 的代码:
final TextView parkAvaliableA = (TextView) findViewById(R.id.parkASlot);
parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
Button buttonA = (Button) findViewById(R.id.parkA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(SlotA.available_slot == 0)
{
Toast.makeText(getApplicationContext(),
"Parking slot A is full",
Toast.LENGTH_SHORT).show();
}
else
{
SlotA.available_slot -= 1;
db.updateSlot(new ParkingSlot());
}
parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
Intent myIntent = new Intent(MainMap.this, ParkA.class);
MainMap.this.startActivity(myIntent);
}
});
当我按下按钮时,应用程序崩溃了。 MainMap.this.startActivity(myIntent);
错了吗?
更新:
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.km.parkit"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".MainMap"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/com.km.parkit" />
</intent-filter>
</activity>
<activity
android:name="com.km.parkit.parka"
android:label="@string/app_name" />
<activity
android:name="com.km.parkit.parkb"
android:label="@string/app_name" />
<activity
android:name="com.km.parkit.parkc"
android:label="@string/app_name" />
<activity
android:name="com.km.parkit.parkd"
android:label="@string/app_name" />
<!-- Google Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBf-0hUDrDTK-NOveZrnT8wec5TTTLqjAw" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
我已经在应用程序中声明了另一个 类,是否缺少某些内容?
将清单中的 Activity 名称更改为
<activity
android:name="com.km.parkit.ParkA"
android:label="@string/app_name" />
而不是com.km.parkit.parka
区分大小写。