java - Android Activity 流程 - 从基父类(super class)到子类再到基类

标签 java android android-activity subclass superclass

作为 Android 新手,我正在尝试构建一个简单的 map 应用程序。我一开始在同一个类上做所有的事情,但由于显而易见的原因,事情很快就失控了。所以我想拆分类,将基类作为 Activity 的主要流程,将子类作为“实用”类。

因此,我实例化了一个子类对象,并在子类的 onCreate 中开始调用方法。但这些方法从未运行过。我究竟做错了什么?一旦我创建了子类对象,子类的 onCreate 就应该触发,不是吗?而且,这是否是在子类而不是整个其他类中执行此操作的明智方法?

提前致谢!

基类:

package com.example.TestMap;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

public class MyActivity extends Activity  {

    private GoogleMap mMap;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationClass locationClass = new LocationClass();



    }

    public void setMap(){
        Log.i("TestMap", "setMap");
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.setMyLocationEnabled(true);

    }



    public void setCamera(Location location) {

        Log.i("TestMap", "setCamera");

        final LatLng locationLatLng = new LatLng( (location.getLatitude() ), location.getLongitude() );


        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(locationLatLng)      // Sets the center of the map to Mountain View
                .zoom(17)                   // Sets the zoom
                .bearing(90)                // Sets the orientation of the camera to east
                .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                .build();                   // Creates a CameraPosition from the builder

        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }

}

子类

package com.example.TestMap;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.util.List;


public class LocationClass extends MyActivity implements LocationListener {

    private LocationManager locationManager;
    private String provider;
    private List<String> providers;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("TestMap", "LocationClass OnCreate");
        GetProivder();
    }


    public void GetProivder (){
        Log.i("TestMap", "LocationClass GetProivder");


        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(2);
        provider = locationManager.getBestProvider(criteria, false);
        providers = locationManager.getProviders(true);

        Location location = locationManager.getLastKnownLocation(provider);

        Log.i("TestMap", "providerlist = " + providers);
        Log.i("TestMap", "getBestProvider = " + provider);
        Log.i("TestMap", "Location = " + location);

        if (location != null) {
            Log.i("TestMap", "Provider " + provider + " has been selected.");
            super.setCamera(location);
            super.setMap();

        } else {
            Log.i("TestMap", "location is null.");

        }
    }


    @Override
    public void onLocationChanged(Location location) {
        Log.i("TestMap", "onLocationChanged");
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i("TestMap", "onStatusChanged");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.i("TestMap", "onProviderDisabled");
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }


}

最佳答案

你的代码

LocationClass locationClass = new LocationClass(); 不会创建它。它只是创建它的一个对象,但它不绑定(bind)到生命周期并调用方法。

您需要以使其出现的 Intent 来启动它(并根据浏览代码占据整个屏幕)。当您执行此操作时,Android 将触发适当的方法调用。

像这样:

Intent intent = new Intent(this, LocationClass.class);          
startActivity(intent);

更多信息可以在这里找到:http://developer.android.com/training/basics/firstapp/starting-activity.html#StartActivity

它也必须在您的 list 中定义,否则会崩溃。您的代码中还有其他明显的奇怪之处,您是否希望 LocationClass 扩展 MyActivity 而不是 Activity ?您的 LocationClass 也不会在 onCreate 中调用 setContentView(),因此您不会看到 UI(据我所知),除非您希望通过 extends 部分看到它。

编辑: 如果您扩展子类并将此 Intent 代码放入 onCreate 中,您可能会崩溃,因为它将在 MyActivity 中调用 super() (再次调用 onCreate() 因为它是父类(super class)),并且将继续发出更多 Intent 来启动 Activity 。如果 MyActivity 是“父”类,那么您不应该对其进行子类化。

您应该只对 Activity 或全局父 Activity 进行子类化(例如,在我现在的项目中,我扩展了 SpiceActivity,因为它们都使用与 Spice 相关的通用组件)。

关于java - Android Activity 流程 - 从基父类(super class)到子类再到基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22387336/

相关文章:

android - 重新创建 Activity 时出现生命周期异常

java - 多个单元格在同一行中对齐(Apache POI)

java - Maven 从中央存储库下载时重命名 jar

android - BSSID 与 MAC 地址?

android - gomobile 回调(将实时下载的内容转发到 android)?

android - 尝试实现 ar core 时无法加载 Renderable

java - 我可以通过检查主 Activity 的 onPause() 中的 isFinish() 来确定 Android 应用程序完成运行吗?

java - ByteArrayOutputStream 对 RAM 内存的极端使用

java - 斯坦福 CoreNLP 管道 coref : parsing some short strings (with few mentions) returns indexoutofbounds exception

java - 关于如何修复 Android 中跳帧的建议