我是Android编程的新手。我正在构建一个应用程序,其中我有一个按钮,通过该按钮可以在点击时打开新活动。但是,当我点击该按钮时,应用程序停止,并且“不幸的是,它已关闭”。
这是我的java文件和清单代码的代码。
package com.example.jamaattiming;
//import android.view.View.OnClickListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
//import android.content.ComponentName;
import android.view.Menu;
import android.view.View;
//import android.content.Context;
import android.widget.Button;
public class MainPage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
Button Qibla=(Button) findViewById(R.id.btnQibla);
Qibla.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startActivity(new Intent("com.example.COMPASS"));
Intent intent = new Intent(MainPage.this, Qibla.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_page, menu);
return true;
}
}
清单:
<activity
android:name=".Qibla"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>`
这是LogCat错误。
12-29 14:45:31.810: I/Process(1705): Sending signal. PID: 1705 SIG: 9
12-29 14:53:11.880: D/dalvikvm(1756): GC_FOR_ALLOC freed 62K, 7% free 2845K/3028K, paused 35ms, total 37ms
12-29 14:53:11.880: I/dalvikvm-heap(1756): Grow heap (frag case) to 3.612MB for 756788-byte allocation
12-29 14:53:11.941: D/dalvikvm(1756): GC_FOR_ALLOC freed 2K, 5% free 3581K/3768K, paused 52ms, total 53ms
12-29 14:53:12.420: D/gralloc_goldfish(1756): Emulator without GPU emulation detected.
12-29 14:53:17.419: D/dalvikvm(1756): GC_FOR_ALLOC freed 13K, 4% free 3639K/3772K, paused 189ms, total 211ms
12-29 14:53:17.469: I/dalvikvm-heap(1756): Grow heap (frag case) to 4.568MB for 945616-byte allocation
12-29 14:53:17.720: D/dalvikvm(1756): GC_FOR_ALLOC freed 1K, 3% free 4561K/4696K, paused 224ms, total 224ms
12-29 14:53:20.159: I/Choreographer(1756): Skipped 35 frames! The application may be doing too much work on its main thread.
12-29 14:53:22.749: D/AndroidRuntime(1756): Shutting down VM
12-29 14:53:22.780: W/dalvikvm(1756): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
12-29 14:53:22.960: E/AndroidRuntime(1756): FATAL EXCEPTION: main
12-29 14:53:22.960: E/AndroidRuntime(1756): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jamaattiming/com.example.jamaattiming.Qibla}: java.lang.ClassCastException: com.example.jamaattiming.Qibla cannot be cast to android.app.Activity
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.os.Looper.loop(Looper.java:137)
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-29 14:53:22.960: E/AndroidRuntime(1756): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 14:53:22.960: E/AndroidRuntime(1756): at java.lang.reflect.Method.invoke(Method.java:525)
12-29 14:53:22.960: E/AndroidRuntime(1756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-29 14:53:22.960: E/AndroidRuntime(1756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 14:53:22.960: E/AndroidRuntime(1756): at dalvik.system.NativeStart.main(Native Method)
12-29 14:53:22.960: E/AndroidRuntime(1756): Caused by: java.lang.ClassCastException: com.example.jamaattiming.Qibla cannot be cast to android.app.Activity
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-29 14:53:22.960: E/AndroidRuntime(1756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-29 14:53:22.960: E/AndroidRuntime(1756): ... 11 more
12-29 14:53:26.179: I/Process(1756): Sending signal. PID: 1756 SIG: 9
答案 0 :(得分:0)
您的按钮对象必须setOncCickListner
而不是其ID。使用此:
btn.setOnClickListener(new View.OnClickListener()
而不是:
Qibla.setOnClickListener(new View.OnClickListener()
答案 1 :(得分:0)
尝试
Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainPage.this, Compass.class);
startActivity(intent);
}
});
答案 2 :(得分:0)
这样做
Button Qibla =(Button) findViewById(R.id.btnQibla);
而不是
Button Btn=(Button) findViewById(R.id.btnQibla);
我希望您已在清单文件中定义指南针活动!
// import android.view.View.OnClickListener;
答案 3 :(得分:0)
更改
Qibla.setOnClickListener(new View.OnClickListener() {
到
btn.setOnClickListener(new View.OnClickListener() {