我正在开发一款用于测量信号强度的应用程序。我在代码中没有错误,但是当我运行应用程序时,我得到一个错误,说明"应用程序(应用程序名称)程序包名称已意外停止"。在此我附加了xml文件,java文件和android清单文件。请尽快回复。
XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/ref"
android:textSize="30sp"
/>
<Button
android:id="@+id/bfind"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:text="@string/ref1"
android:layout_gravity="center"
android:textSize="25sp" />
<ProgressBar android:id="@+id/signalLevel" />
<TextView android:id="@+id/signalLevelInfo"/>
</RelativeLayout>
JAVA文件
package com.signalstrength.gsmece;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.*;
import android.telephony.gsm.GsmCellLocation;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button find;
TelephonyManager _telephonyManager;
GsmSignalStrengthListener _signalStrengthListener;
TextView display;
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();
String IMEI = tm.getDeviceId();
int lac = location.getLac();
int cid = location .getCid();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
_signalStrengthListener = new GsmSignalStrengthListener();
find = (Button) findViewById(R.id.bfind);
display = (TextView) findViewById(R.id.textView1) ;
find.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
display.setText("Calculating....");
}
});
}
public abstract class CellSignalStrength extends Object
{
}
@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, menu);
return true;
}
}
预告文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.signalstrength.gsmece"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.signalatrength.gsmece.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
LOGCAT文件
03-16 19:15:16.859: D/AndroidRuntime(277): Shutting down VM
03-16 19:15:16.859: W/dalvikvm(277): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-16 19:15:17.089: E/AndroidRuntime(277): FATAL EXCEPTION: main
03-16 19:15:17.089: E/AndroidRuntime(277): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.signalstrength.gsmece/com.signalstrength.gsmece.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
03-16 19:15:17.089: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
03-16 19:15:17.089: E/AndroidRuntime(277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-16 19:15:17.089: E/AndroidRuntime(277): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-16 19:15:17.089: E/AndroidRuntime(277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-16 19:15:17.089: E/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 19:15:17.089: E/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123)
03-16 19:15:17.089: E/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-16 19:15:17.089: E/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:15:17.089: E/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 19:15:17.089: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-16 19:15:17.089: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-16 19:15:17.089: E/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method)
03-16 19:15:17.089: E/AndroidRuntime(277): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
03-16 19:15:17.089: E/AndroidRuntime(277): at android.app.Activity.getSystemService(Activity.java:3526)
03-16 19:15:17.089: E/AndroidRuntime(277): at com.signalstrength.gsmece.MainActivity.<init>(MainActivity.java:23)
03-16 19:15:17.089: E/AndroidRuntime(277): at java.lang.Class.newInstanceImpl(Native Method)
03-16 19:15:17.089: E/AndroidRuntime(277): at java.lang.Class.newInstance(Class.java:1429)
03-16 19:15:17.089: E/AndroidRuntime(277): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-16 19:15:17.089: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
03-16 19:15:17.089: E/AndroidRuntime(277): ... 11 more
03-16 19:15:25.850: I/Process(277): Sending signal. PID: 277 SIG: 9
03-16 19:15:50.780: D/AndroidRuntime(288): Shutting down VM
03-16 19:15:50.780: W/dalvikvm(288): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-16 19:15:50.810: E/AndroidRuntime(288): FATAL EXCEPTION: main
03-16 19:15:50.810: E/AndroidRuntime(288): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.signalstrength.gsmece/com.signalstrength.gsmece.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
03-16 19:15:50.810: E/AndroidRuntime(288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
03-16 19:15:50.810: E/AndroidRuntime(288): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-16 19:15:50.810: E/AndroidRuntime(288): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-16 19:15:50.810: E/AndroidRuntime(288): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-16 19:15:50.810: E/AndroidRuntime(288): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 19:15:50.810: E/AndroidRuntime(288): at android.os.Looper.loop(Looper.java:123)
03-16 19:15:50.810: E/AndroidRuntime(288): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-16 19:15:50.810: E/AndroidRuntime(288): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:15:50.810: E/AndroidRuntime(288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-16 19:15:50.810: E/AndroidRuntime(288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-16 19:15:50.810: E/AndroidRuntime(288): at dalvik.system.NativeStart.main(Native Method)
03-16 19:15:50.810: E/AndroidRuntime(288): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
03-16 19:15:50.810: E/AndroidRuntime(288): at com.signalstrength.gsmece.MainActivity.<init>(MainActivity.java:23)
03-16 19:15:50.810: E/AndroidRuntime(288): at java.lang.Class.newInstanceImpl(Native Method)
03-16 19:15:50.810: E/AndroidRuntime(288): at java.lang.Class.newInstance(Class.java:1429)
03-16 19:15:50.810: E/AndroidRuntime(288): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-16 19:15:50.810: E/AndroidRuntime(288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
03-16 19:15:50.810: E/AndroidRuntime(288): ... 11 more
03-16 19:20:50.879: I/Process(288): Sending signal. PID: 288 SIG: 9
答案 0 :(得分:2)
启动主活动时,清单文件包名错误。替换
android:name="com.signalatrength.gsmece.MainActivity"
与
android:name="com.signalstrength.gsmece.MainActivity"
答案 1 :(得分:0)
在调用onCreate之前,您无法调用getSystemServices。将获取TelephonyManager的行和依赖它的行移动到onCreate