0

我正在开发一个应用程序来显示网络提供商的所有详细信息,但是我遇到了错误,任何人都可以帮我清除它吗?

logcat 文件:

D/AndroidRuntime(308): Shutting down VMW/dalvikvm(308):threadid=1: thread exiting with  
uncaughtexception(group=0x4001d800)
E/AndroidRuntime(308): FATAL EXCEPTION: main
E/AndroidRuntime(308): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.LocationTest/com.LocationTest.LocationTestActivity}: 
java.lang.NullPointerException
E/AndroidRuntime(308):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
E/AndroidRuntime(308):at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(308):at 
android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(308):at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(308):at 
android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(308):at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(308)at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(308):at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(308):at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(308):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(308):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(308):at dalvik.system.NativeStart.main(Native 
Method)
E/AndroidRuntime(308): Caused by: java.lang.NullPointerException
E/AndroidRuntime(308):at android.app.Activity.findViewById(Activity.java:1637)
E/AndroidRuntime(308):at com.LocationTest.LocationTestActivity.
<init>(LocationTestActivity.java:18)
E/AndroidRuntime(308):at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(308):at java.lang.Class.newInstance(Class.java:1429)
E/AndroidRuntime(308):at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(308):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
E/AndroidRuntime(308):  ... 11 more
I/Process(308): Sending signal. PID: 308 SIG: 9

代码:

public class LocationTestActivity extends Activity implements LocationListener {
/** Called when the activity is first created. */
 LocationManager locman;
 String best; Location loc; TextView t=(TextView)findViewById(R.id.text);
 private static final String[] A = { "invalid" , "n/a" , "fine" , "coarse" };
 private static final String[] P = { "invalid" , "n/a" , "low" , "medium" ,
 "high" };
 private static final String[] S = { "out of service" ,
 "temporarily unavailable" , "available" };
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   locman =(LocationManager)getSystemService(LOCATION_SERVICE);

    log("location Providers");
    dumpproviders();
    Criteria criteria=new Criteria();
    best=locman.getBestProvider(criteria,true);
    log("BestProviders"+best);
    log("Starting with last known location");
    loc=locman.getLastKnownLocation(best);
    dumpLocation(loc); 

}
protected void OnResume()
{
   super.onPause();
locman.requestLocationUpdates(best, 15000,1,this);
}
 protected void Onpause()
 {
    super.onPause();
    locman.removeUpdates(this);
 }



private void dumpproviders() {
    // TODO Auto-generated method stub
    List<String> providers=locman.getAllProviders();
    for(String provider:providers){
        dumpProvider(provider);
    }
}

private void dumpProvider(String providers) {
    // TODO Auto-generated method stub
    LocationProvider info=locman.getProvider(providers);
    StringBuilder buil=new StringBuilder();
    buil.append("LocationProvier[")
    .append("name=")
    .append(info.getName())
    .append("provider state")
    .append(locman.isProviderEnabled(providers))
    .append(",getAccuracy=" )
    .append(A[info.getAccuracy() + 1])
    .append(",getPowerRequirement=" )
    .append(P[info.getPowerRequirement() + 1])
    .append(",hasMonetaryCost=" )
    .append(info.hasMonetaryCost())
    .append(",requiresCell=" )
    .append(info.requiresCell())
    .append(",requiresNetwork=" )
    .append(info.requiresNetwork())
    .append(",requiresSatellite=" )
    .append(info.requiresSatellite())
    .append(",supportsAltitude=" )
    .append(info.supportsAltitude())
    .append(",supportsBearing=" )
    .append(info.supportsBearing())
    .append(",supportsSpeed=" )
    .append(info.supportsSpeed())
    .append("]" );
    log(buil.toString());
}
private void log(String string) {
    // TODO Auto-generated method stub
    t.append(string+"\n");
}
@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    dumpLocation(loc);
}
private void dumpLocation(Location loc) {
    // TODO Auto-generated method stub
    if (loc == null)
        log("\nLocation[unknown]" );
        else
        log("\n" + loc.toString());
}
@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    log("\nProvider disabled: " + arg0);
}
@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub
    log("\nProvider enabled: " + arg0);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    log("\nProvider status changed: " + provider + ", status="
            + S[status] + ", extras=" + extras);
}
4

1 回答 1

1

您不能在类范围内拥有以下代码行。

TextView t=(TextView)findViewById(R.id.text);

findViewByID 方法仅在特定 View 已显示在屏幕上时才返回有效的 View 对象,否则它将返回 NULL。

在您的情况下,您甚至在 TextView 可见之前就尝试访问它,因此您得到 NullPointerException (检查日志中引发的第二个异常)

E/AndroidRuntime(308): Caused by: java.lang.NullPointerException
E/AndroidRuntime(308):at android.app.Activity.findViewById(Activity.java:1637)
E/AndroidRuntime(308):at com.LocationTest.LocationTestActivity. (LocationTestActivity.java:18)
E/AndroidRuntime(308):at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(308):at java.lang.Class.newInstance(Class.java:1429)
E/AndroidRuntime(308):at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(308):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
E/AndroidRuntime(308): ... 11 more I/Process(308): Sending signal. PID: 308 SIG: 9

在“setContentView(R.layout.main);”之后添加上述行 在 onCreate() 方法中它应该可以工作,例如...

setContentView(R.layout.main);
t = (TextView) findViewByID(R.id.text);

仔细阅读错误日志,您将获得调试所需的所有信息。

于 2012-06-04T13:05:21.930 回答