如何使用Android中的按钮开始新活动?
这是我的应用中按钮“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>
我在应用程序中声明了其他类,是否缺少某些内容?
答案 0 :(得分:1)
将您的活动名称更改为
<activity
android:name="com.km.parkit.ParkA"
android:label="@string/app_name" />
而不是com.km.parkit.parka
区分大小写。