2016-07-31 54 views
0

我想了解更多關於Java的知識。我對這件事一無所知。人們在說,如果我知道PHP,我就會認識Java。我不這麼認爲。 Java太多了,並且不允許任何例外。Android開發(Java) - 崩潰

我的應用程序在啓動時立即崩潰。我已經爲應用程序的權限添加了「FINE_LOCATION」和「COARSE_LOCATION」。

package either.no.me.myapplication2; 

import android.Manifest; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

import java.util.Timer; 
import java.util.TimerTask; 

public class MainActivity extends AppCompatActivity { 

    TextView textLat; 
    TextView textLon; 
    Timer timer1 = new Timer(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textLat = (TextView) findViewById(R.id.textLat); 
     textLon = (TextView) findViewById(R.id.textLon); 
     GPSFinder gpsFinder = new GPSFinder(); 
     gpsFinder.GetLocation(); 
    } 
} 

class GPSFinder extends MainActivity { 
    public void GetLocation() { 
     LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     // Define a listener that responds to location updates 
     LocationListener locationListener = new LocationListener() { 
      public void onLocationChanged(Location location) { 
       // Called when a new location is found by the network location provider. 
       textLat.setText(Double.toString(location.getLatitude())); 
       textLon.setText(Double.toString(location.getLongitude())); 
      } 

      public void onStatusChanged(String provider, int status, Bundle extras) { 
      } 

      public void onProviderEnabled(String provider) { 
      } 

      public void onProviderDisabled(String provider) { 
      } 
     }; 

     // Register the listener with the Location Manager to receive location updates 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    } 
} 

我得到的logcat的錯誤:

07-31 01:36:45.611 1703-1703/either.no.me.myapplication2 E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: either.no.me.myapplication2, PID: 1703 
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{either.no.me.myapplication2/either.no.me.myapplication2.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate() 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) 
                       at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:136) 
                       at android.app.ActivityThread.main(ActivityThread.java:5045) 
                       at java.lang.reflect.Method.invokeNative(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:515) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                       at dalvik.system.NativeStart.main(Native Method) 
                      Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate() 
                       at android.app.Activity.getSystemService(Activity.java:4532) 
                       at either.no.me.myapplication2.GPSFinder.GetLocation(MainActivity.java:37) 
                       at either.no.me.myapplication2.MainActivity.onCreate(MainActivity.java:31) 
                       at android.app.Activity.performCreate(Activity.java:5231) 
                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)  
                       at android.app.ActivityThread.access$800(ActivityThread.java:135)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  
                       at android.os.Handler.dispatchMessage(Handler.java:102)  
                       at android.os.Looper.loop(Looper.java:136)  
                       at android.app.ActivityThread.main(ActivityThread.java:5045)  
                       at java.lang.reflect.Method.invokeNative(Native Method)  
                       at java.lang.reflect.Method.invoke(Method.java:515)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  
                       at dalvik.system.NativeStart.main(Native Method)  
07-31 01:36:48.875 1703-1703/either.no.me.myapplication2 I/Process: Sending signal. PID: 1703 SIG: 9 

回答

0

刪除GPSFinder類和內MainActivity 的getLocation方法的使用代碼像下面

<pre> 
package either.no.me.myapplication2; 

import android.Manifest; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

import java.util.Timer; 
import java.util.TimerTask; 

public class MainActivity extends AppCompatActivity { 

    TextView textLat; 
    TextView textLon; 
    Timer timer1 = new Timer(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textLat = (TextView) findViewById(R.id.textLat); 
     textLon = (TextView) findViewById(R.id.textLon); 
     getLocation(); 
    } 

public void getLocation() { 
     LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     // Define a listener that responds to location updates 
     LocationListener locationListener = new LocationListener() { 
      public void onLocationChanged(Location location) { 
       // Called when a new location is found by the network location provider. 
       textLat.setText(Double.toString(location.getLatitude())); 
       textLon.setText(Double.toString(location.getLongitude())); 
      } 

      public void onStatusChanged(String provider, int status, Bundle extras) { 
      } 

      public void onProviderEnabled(String provider) { 
      } 

      public void onProviderDisabled(String provider) { 
      } 
     }; 

     // Register the listener with the Location Manager to receive location updates 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    } } 
+0

現在我刪除了類,並使用了MainActivity中的代碼,就像我可以像這樣使用void:GetLocation();但現在的錯誤看起來是這樣的:https://i.gyazo.com/35af174610f8c1e73f230c4c92207c0c.png –

+0

我同意這種做法,他應該採取修復。但是ID說他的代碼崩潰的原因是因爲他擴展了一個Activity類,並且在其onCreate()之前調用了一個需要上下文的方法,因此被稱爲 –

+0

因此,我該怎麼做。我是Android開發人員的超級新手,我還沒有弄明白。我可以在onCreate完成初始化後使用void來調用它嗎?編輯:我把getLocation替換爲getLocation,它開始工作,但我使用Nox作爲模擬器進行調試,但它不包含「網絡」,所以我得到這個:「java.lang.IllegalArgumentException:提供者不存在:網絡「我必須解決它才能繼續。謝謝你的朋友。 –

0

GPSFinder不應MainActivity派生。這不是一個活動。

此外,活動不應該通過新創建 - 他們不會被完全初始化。他們應該開始(或者代碼不應該像這裏一樣是一個活動)。因爲它在調用getSystemServices時沒有正確初始化,所以它失敗了。如果你需要一個上下文或系統服務,它應該被傳入,你不應該從一個Context類派生。