大家好,这是我的第一款Android应用。我是Java和Eclipse的新手,所以请原谅任何“noobity”。 我创建了这个项目,并尝试在我的设备上运行它(nexus s)。这就是我在LogCat中得到的结果。
12-17 21:03:11.550: E/AndroidRuntime(20786): FATAL EXCEPTION: main
12-17 21:03:11.550: E/AndroidRuntime(20786): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gsmdata/com.example.gsmdata.GsmDataActivity}: java.lang.NullPointerException
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.app.ActivityThread.access$1500 (ActivityThread.java:117)
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.os.Looper.loop(Looper.java:130)
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-17 21:03:11.550: E/AndroidRuntime(20786): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 21:03:11.550: E/AndroidRuntime(20786): at java.lang.reflect.Method.invoke(Method.java:507)
12-17 21:03:11.550: E/AndroidRuntime(20786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-17 21:03:11.550: E/AndroidRuntime(20786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-17 21:03:11.550: E/AndroidRuntime(20786): at dalvik.system.NativeStart.main(Native Method)
12-17 21:03:11.550: E/AndroidRuntime(20786): Caused by: java.lang.NullPointerException
12-17 21:03:11.550: E/AndroidRuntime(20786): at com.example.gsmdata.GsmDataActivity.onCreate(GsmDataActivity.java:72)
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-17 21:03:11.550: E/AndroidRuntime(20786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-17 21:03:11.550: E/AndroidRuntime(20786): ... 11 more
public class GsmDataActivity extends Activity {
/** Called when the activity is first created. */
int[] SignalStrength = {
-113,
-111,
-109,
-107,
-105,
-103,
-101,
-99,
-97,
-95,
-93,
-91,
-89,
-87,
-85,
-83,
-81,
-79,
-77,
-75,
-73,
-71,
-69,
-67,
-65,
-63,
-61,
-59,
-57,
-55,
-53,
-51};
int network_type,signal_strength,cid,Lac,bts;
TextView network, signal, ci, lac;
TelephonyManager phone;
GsmCellLocation cellId;
CdmaCellLocation baseStation;
SignalStrength phone_sig;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
network = (TextView) findViewById(R.id.tvNetwork);
signal =(TextView) findViewById(R.id.tvSignalValue);
ci = (TextView) findViewById(R.id.tvCiValue);
lac = (TextView) findViewById(R.id.tvLacValue);
network_type=phone.getNetworkType();
switch (network_type){
case 1: network.setText("GPRS");
case 3: network.setText("UMTS");
case 4: network.setText("CDMA");
case 8: network.setText("HSDPA");
case 9: network.setText("HSUPA");
case 10: network.setText("HSPA");
case 13: network.setText("LTE");
default: network.setText("UNKNOWN");
}
if (network_type==1){
signal_strength = phone_sig.getGsmSignalStrength();
signal.setText(SignalStrength[signal_strength] + "dBm");
cid= cellId.getCid();
Lac=cellId.getLac();
}
else if(network_type==0){
signal.setText("Unknown Network type");
}
else{
signal_strength= phone_sig.getCdmaDbm();
signal.setText(SignalStrength[signal_strength] + "dBm");
bts= baseStation.getBaseStationId();
Lac=cellId.getLac();
}
}
}
这是我的java类。我在主要部分获得了致命的异常。我不知道为什么。你能帮助我吗。先感谢您。以下是xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width= "fill_parent"
android:layout_height="wrap_content"
android:textSize="50dp"
android:paddingBottom="5dp"
android:id="@+id/tvNetwork"
></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/strength"
android:layout_gravity="center"
android:paddingBottom="5dp"
android:id="@+id/tvSignal"
/>
<TextView
android:layout_width= "fill_parent"
android:layout_height="wrap_content"
android:textSize="50dp"
android:paddingBottom="5dp"
android:gravity="center"
android:id="@+id/tvSignalValue"
></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cellid"
android:layout_gravity="center"
android:paddingBottom="5dp"
android:id="@+id/tvCi"
/>
<TextView
android:layout_width= "fill_parent"
android:layout_height="wrap_content"
android:textSize="50dp"
android:paddingBottom="5dp"
android:gravity="center"
android:id="@+id/tvCiValue"
></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/lac"
android:layout_gravity="center"
android:id="@+id/tvLac"
/>
<TextView
android:layout_width= "fill_parent"
android:layout_height="wrap_content"
android:textSize="50dp"
android:paddingBottom="5dp"
android:gravity="center"
android:id="@+id/tvLacValue"
></TextView>
</LinearLayout>
答案 0 :(得分:1)
phone
时, phone.getNetworkType();
为null,因此你得到NullPointerException。
首先将电话管理员分配给它:
phone = getSystemService(Context.TELEPHONY_SERVICE);
phone.getNetworkType();