我正在编写一个android应用程序,该应用程序有很多按钮,每个按钮都会将用户引导到另一个活动,问题是当我运行应用程序时它会崩溃。
它只是说:不幸的是,该项目已经停止。
我不知道可能导致这个问题的原因!!
这是我正在使用的主要活动:
public class MainActivity extends DashActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setHeader(getString(R.string.HomeTitle), true);
}
public void onButtonClicker(View v)
{
Intent intent;
switch (v.getId()) {
case R.id.hotels_bt:
intent = new Intent(this, Hotels.class);
startActivity(intent);
break;
case R.id.restaurants_bt:
intent = new Intent(this, Restaurants.class);
startActivity(intent);
break;
case R.id.airports_bt:
intent = new Intent(this, Airports.class);
startActivity(intent);
break;
case R.id.currency_bt:
intent = new Intent(this, Currency.class);
startActivity(intent);
break;
case R.id.praytime_bt:
intent = new Intent(this, PrayTime.class);
startActivity(intent);
break;
case R.id.about_bt:
intent = new Intent(this, About.class);
startActivity(intent);
break;
default:
break;
}
}
}
这是其他活动之一:仅用于测试:
public class Airports extends DashActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.airports);
setHeader(getString(R.string.airports), true);
}
}
这是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" >
<!-- Included header.xml here -->
<ViewStub android:id="@+id/vsHeader"
android:inflatedId="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout="@layout/header" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="6dip">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/hotels_bt"
style="@style/home_button"
android:layout_width="122dp"
android:drawableTop="@drawable/hotel"
android:onClick="onButtonClicker"
android:text="@string/hotels" />
<Button
android:id="@+id/restaurants_bt"
style="@style/home_button"
android:layout_width="133dp"
android:drawableTop="@drawable/restaurant"
android:onClick="onButtonClicker"
android:text="@string/restaurants" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/airports_bt"
style="@style/home_button"
android:drawableTop="@drawable/airport"
android:onClick="onButtonClicker"
android:text="@string/airports" />
<Button android:id="@+id/currency_bt"
style="@style/home_button"
android:onClick="onButtonClicker"
android:text="@string/currency"
android:drawableTop="@drawable/dollar"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button android:id="@+id/praytime_bt"
style="@style/home_button"
android:onClick="onButtonClicker"
android:text="@string/praytime"
android:drawableTop="@drawable/mosque"/>
<Button android:id="@+id/about_bt"
style="@style/home_button"
android:onClick="onButtonClicker"
android:text="@string/about"
android:drawableTop="@drawable/about"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是logcat:
11-04 16:05:20.490: E/AndroidRuntime(2196): FATAL EXCEPTION: main
11-04 16:05:20.490: E/AndroidRuntime(2196): java.lang.IllegalStateException: Could not execute method of the activity
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.view.View$1.onClick(View.java:3633)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.view.View.performClick(View.java:4240)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.view.View$PerformClick.run(View.java:17721)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.os.Handler.handleCallback(Handler.java:730)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.os.Handler.dispatchMessage(Handler.java:92)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.os.Looper.loop(Looper.java:137)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 16:05:20.490: E/AndroidRuntime(2196): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 16:05:20.490: E/AndroidRuntime(2196): at java.lang.reflect.Method.invoke(Method.java:525)
11-04 16:05:20.490: E/AndroidRuntime(2196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 16:05:20.490: E/AndroidRuntime(2196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 16:05:20.490: E/AndroidRuntime(2196): at dalvik.system.NativeStart.main(Native Method)
11-04 16:05:20.490: E/AndroidRuntime(2196): Caused by: java.lang.reflect.InvocationTargetException
11-04 16:05:20.490: E/AndroidRuntime(2196): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 16:05:20.490: E/AndroidRuntime(2196): at java.lang.reflect.Method.invoke(Method.java:525)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.view.View$1.onClick(View.java:3628)
11-04 16:05:20.490: E/AndroidRuntime(2196): ... 11 more
11-04 16:05:20.490: E/AndroidRuntime(2196): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.dashboard_our/com.example.dashboard_our.Hotels}; have you declared this activity in your AndroidManifest.xml?
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.app.Activity.startActivityForResult(Activity.java:3390)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.app.Activity.startActivityForResult(Activity.java:3351)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.app.Activity.startActivity(Activity.java:3587)
11-04 16:05:20.490: E/AndroidRuntime(2196): at android.app.Activity.startActivity(Activity.java:3555)
11-04 16:05:20.490: E/AndroidRuntime(2196): at com.example.dashboard_our.MainActivity.onButtonClicker(MainActivity.java:24)
11-04 16:05:20.490: E/AndroidRuntime(2196): ... 14 more
答案 0 :(得分:0)
尝试以下方法:
public void onButtonClicker(View v)
{
Intent intent;
Button b1 = (Button)findViewById(R.id.hotels_bt);
.
.
switch (v.getId()) {
case (b1.getId()):
intent = new Intent(this, Hotels.class);
startActivity(intent);
break;
.
.
.
default:
break;
}
}