2016-03-22 19 views
0

割り当てることができませんでした。 Android Wearデバイス用のAndroidアプリを作りたい私は現在の場所の緯度と経度を取得したい。Android Wearアプリのjava.lang.OutOfMemoryErrorを:私は問題を抱えていた

MainActivity.java

public class MainActivity extends Activity { 

private TextView tvHuidigeLocatie; 
private Button btSetHuidigeLocatie; 

GPSTracker gps; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); 
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { 
     @Override 
     public void onLayoutInflated(WatchViewStub stub) { 


      onClickInfoButtonListener(); 
     } 
    }); 
} 

private void onClickInfoButtonListener() { 
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); 
    tvHuidigeLocatie = (TextView) stub.findViewById(R.id.tvHuidigeLocatie); 
    btSetHuidigeLocatie = (Button) stub.findViewById(R.id.btnSetHuidigeLocatie); 
    btSetHuidigeLocatie.setOnClickListener(
      new View.OnClickListener(){ 
       @Override 
       public void onClick(View v){ 
        gps = new GPSTracker(MainActivity.this); 

        if(gps.canGetLocation()){ 
         double latitude = gps.getLatitude(); 
         double longitude = gps.getLongitude(); 

         tvHuidigeLocatie.setText("" + latitude + ", " + longitude); 
        }else{ 
         gps.showSettingsAlert(); 
        } 
       } 
      } 
    ); 
} 

}

GPSTracker.java

:これは私のコードは

03-22 11:14:00.096 29013-29013/? E/AndroidRuntime: Error reporting crash 
               java.lang.OutOfMemoryError: Failed to allocate a 52435096 byte allocation with 2097152 free bytes and 32MB until OOM 
                at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95) 
                at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125) 
                at java.lang.StringBuffer.append(StringBuffer.java:278) 
                at java.io.StringWriter.write(StringWriter.java:123) 
                at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358) 
                at com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303) 
                at com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625) 
                at com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658) 
                at java.io.PrintWriter.append(PrintWriter.java:691) 
                at java.io.PrintWriter.append(PrintWriter.java:687) 
                at java.io.Writer.append(Writer.java:198) 
                at java.lang.Throwable.printStackTrace(Throwable.java:324) 
                at java.lang.Throwable.printStackTrace(Throwable.java:300) 
                at android.util.Log.getStackTraceString(Log.java:343) 
                at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:61) 
                at com.android.internal.os.RuntimeInit.-wrap0(RuntimeInit.java) 
                at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:86) 
                at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) 
                at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 

です:私はapplicatieを実行したときしかし、私はこのエラーを得ました

public class GPSTracker extends Service implements LocationListener { 

private final Context context; 

boolean isGPSEnabled = false; 
boolean isNetworkEnabled = false; 
boolean canGetLocation = false; 

Location location; 

double latitude; 
double longitude; 

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; 

protected LocationManager locationManager; 

public GPSTracker(Context context) { 
    this.context = context; 
    getLocation(); 
} 

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE); 
     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 

     } else { 
      this.canGetLocation = true; 

      if (isNetworkEnabled) { 

       if (Build.VERSION.SDK_INT >= 23 && 
         ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
         ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        return location; 
       } 

       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

       if (locationManager != null) { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

        if (locationManager != null) { 
         location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER); 

         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return location; 
} 

public void stopUsingGPS() { 
    if (locationManager != null) { 

     locationManager.removeUpdates(GPSTracker.this); 
    } 
} 

public double getLatitude(){ 
    if(location != null){ 
     latitude = location.getLatitude(); 
    } 

    return latitude; 
} 

public double getLongitude(){ 
    if(location != null){ 
     longitude = location.getLongitude(); 
    } 

    return longitude; 
} 

public boolean canGetLocation(){ 
    return this.canGetLocation(); 
} 

public void showSettingsAlert(){ 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 

    alertDialog.setTitle("GPS in settings"); 

    alertDialog.setMessage("GPS staat niet aan, Wilt u naar uw instellingen gaan?"); 

    alertDialog.setPositiveButton("Instellingen", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      context.startActivity(intent); 
     } 
    }); 

    alertDialog.setNegativeButton("Annuleer", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 

    alertDialog.show(); 
} 

@Override 
public void onLocationChanged(Location location) { 

} 

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

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

}

は、私は誰が問題を見ると、この問題で私を助けることができると思います。

+0

この問題は申し訳ありません –

+0

を発生している場所を正確についてもう少し特定してください! btSetHuidigeLocatieボタンをクリックすると、Android Studioはこの問題をログに記録します。 –

+0

デバッガを使用して、ボタンが押された後でこれが表示されるまでステッピングを維持します。それが問題のラインになります。それが問題を見つけるのに役立ちます。 –

答えて

-1
android:largeHeap="true" 

あなたのアンドロイドマニフェストでは、これがあなたの問題を解決することを願っています。 http://developer.android.com/guide/topics/manifest/application-element.html#largeHeap

+0

いくつかのデバイスで動作するかもしれませんが、最高のスタイルではありませんか? 'ほとんどのアプリはこれを必要とすべきではなく、全体のメモリ使用量を減らしてパフォーマンスを向上させることに焦点を当てるべきです。これを有効にしても、使用可能なメモリが固定されていることが保証されるわけではありません。一部のデバイスは使用可能なメモリの合計によって制限されているためです。 ' –

+1

これはまったく良い方法ではありません。 –

+0

私もこれを追加しますが、それは役に立ちません。 –

関連する問題