我正在按照教程创建一个应用程序来测量 GSM 信号强度。我的文件中没有错误。但我的应用程序不会在设备中运行。我附上了我的代码。有人能找出错误在哪里吗?
我的主要活动代码
package com.example.getgsmsignalstrength;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class MainActivity extends Activity
{
TelephonyManager Tel;
MyPhoneStateListener MyListener;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Update the listener, and start it */
MyListener = new MyPhoneStateListener();
Tel = ( TelephonyManager
)getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
/* Called when the application is minimized */
@Override
protected void onPause()
{
super.onPause();
Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
}
/* Called when the application resumes */
@Override
protected void onResume()
{
super.onResume();
Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
/* —————————– */
/* Start the PhoneState listener */
/* —————————– */
private class MyPhoneStateListener extends PhoneStateListener
{
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = "
+ String.valueOf(signalStrength.getGsmSignalStrength()),
Toast.LENGTH_SHORT).show();
}
};/* End of private Class */
}/* GetGsmSignalStrength */
我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.getgsmsignalstrength"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"></uses-sdk>
<uses-permission
android:name="android.permission.CHANGE_NETWORK_STATE">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".getgsmsignalstrength"
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>
我的主要:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
我的 logcat 显示:
06-19 12:04:20.843: D/AndroidRuntime(24717): Shutting down VM
06-19 12:04:20.843: W/dalvikvm(24717): threadid=1: thread exiting with uncaught
exception (group=0x40018578)
06-19 12:04:20.843: E/AndroidRuntime(24717): FATAL EXCEPTION: main
06-19 12:04:20.843: E/AndroidRuntime(24717): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.getgsmsignalstrength/com.example.getgsmsignalstrength.getgsmsignalstrength}: java.lang.ClassNotFoundException: com.example.getgsmsignalstrength.getgsmsignalstrength in loader dalvik.system.PathClassLoader[/data/app/com.example.getgsmsignalstrength-2.apk]
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.os.Looper.loop(Looper.java:130)
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-19 12:04:20.843: E/AndroidRuntime(24717): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 12:04:20.843: E/AndroidRuntime(24717): at java.lang.reflect.Method.invoke(Method.java:507)
06-19 12:04:20.843: E/AndroidRuntime(24717): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-19 12:04:20.843: E/AndroidRuntime(24717): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-19 12:04:20.843: E/AndroidRuntime(24717): at dalvik.system.NativeStart.main(Native Method)
06-19 12:04:20.843: E/AndroidRuntime(24717): Caused by: java.lang.ClassNotFoundException: com.example.getgsmsignalstrength.getgsmsignalstrength in loader dalvik.system.PathClassLoader[/data/app/com.example.getgsmsignalstrength-2.apk]
06-19 12:04:20.843: E/AndroidRuntime(24717): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
06-19 12:04:20.843: E/AndroidRuntime(24717): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
06-19 12:04:20.843: E/AndroidRuntime(24717): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-19 12:04:20.843: E/AndroidRuntime(24717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
06-19 12:04:20.843: E/AndroidRuntime(24717): ... 11 more