科特林的塞特犬和吸气鬼

时间:2018-01-14 16:57:54

标签: android kotlin

我正在尝试在按下按钮时从另一个Activity更改trx的值,我尝试的是

ScoresActivity.kt

class ScoresActivity : AppCompatActivity(), View.OnClickListener {

var trx = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_scores)
  }
}

其他课程

override fun onClick(p0: View?) {

    when(p0){

        button_1 -> {
            ScoresActivity().trx = 5
        }
}

但每当我打印trx的值时,它就是0。

2 个答案:

答案 0 :(得分:4)

问题:

当您致电ScoresActivity()时,您将创建ScoresActivity课程的新实例。由于您没有保存此实例,因此值trx的设置将丢失。

解决方案: 我不确定trx应该代表什么,但由于Activity名称给出了一个提示,我认为它是某种分数(然后可以在全球范围内使用)。

一种方法是将trx放在companion object中,而class ScoresActivity: Activity() { // ... companion object { var trx: Int = 0 } } 本身就在ScoresActivity中。

ScoresActivity.trx = 5

用法(来自任何活动):

trx

其他方式是另一个类,它是一个单例(Kotlin中的object)并表示构成游戏状态的值,或者通过活动之间的意图传递 var logoElement = document.createElement ('a'); logoElement.href = 'http://www.schienenpost.de/'; logoElement.target = '_blank'; var logoImage = document.createElement ('img'); logoImage.src = 'http://www.schienenpost.de/schienenpost.png'; logoElement.appendChild (logoImage); var iconStyle = new ol.style.Style ({ image: new ol.style.Icon (/** @type {olx.style.IconOptions} */ ({ anchor: [0.5, 1.0], src: 'http://www.schienenpost.de/marker/marker.png' })) }); var scaleLineControl = new ol.control.ScaleLine(); var markerStartEnd = function (layer,feature) { var i, iconFeature, iconFeatures = [], coordinates, nameLine; coordinates = feature.getGeometry ().getCoordinates (); nameLine = feature.getProperties () ['name']; i = 0; iconFeature = new ol.Feature ({ geometry: new ol.geom.Point (coordinates[i]), name: 'Start'+nameLine, }); iconFeature.setStyle (iconStyle); iconFeatures.push (iconFeature); i = coordinates.length - 1; iconFeature = new ol.Feature ({ geometry: new ol.geom.Point (coordinates[i]), name: 'End'+nameLine, }); iconFeature.setStyle (iconStyle); iconFeatures.push (iconFeature); layer.getSource ().addFeatures (iconFeatures); }; var layerLines = new ol.layer.Vector ({ source: new ol.source.Vector ({ format: new ol.format.GeoJSON (), url: 'schienenpost.geojson', useSpatialIndex: false }), style: new ol.style.Style ({stroke: new ol.style.Stroke ({color : 'red', width: 3})}), }); var layerMarker = new ol.layer.Vector ({ title: 'Marker', source: new ol.source.Vector () }); var element = document.getElementById ('popup'); var popup = new ol.Overlay ({ element: element, positioning: 'bottom-center', stopEvent: false, offset: [0, -0] }); var map = new ol.Map ({ controls: ol.control.defaults () .extend ([ new ol.control.OverviewMap (), new ol.control.FullScreen (), scaleLineControl ]), //target 'map', target: document.getElementById ('map'), layers: [ new ol.layer.Tile ({ source: new ol.source.OSM () }), ], view: new ol.View ({ center: ol.proj.fromLonLat ([10.627, 53.620]), zoom: 8 }), logo: logoElement }); map.addOverlay (popup); map.addLayer (layerLines); map.addLayer (layerMarker); map.once ('moveend', function(e) { layerLines.getSource ().getFeaturesCollection ().forEach (function (feature) { markerStartEnd (layerMarker,feature); }); }); map.on ('click', function (evt) { var feature = map.forEachFeatureAtPixel (evt.pixel, function (feature) { return feature; }); if (feature) { var coordinates = feature.getGeometry ().getCoordinates (); var clickpoint = map.getCoordinateFromPixel (evt.pixel); if (!isNaN (coordinates [0])) { // Punkt popup.setPosition (coordinates); } else if (!isNaN (coordinates [0][0])) { // Linie popup.setPosition (clickpoint); } else { // kein brauchbares feature $ (element).popover ('destroy'); return; } $ (element).popover ({ 'placement': 'top', 'html': true, 'content': feature.get ('name') }); $ (element).popover ().data ('bs.popover').options.content = feature.get ('name'); $ (element).popover ('show'); } else { $ (element).popover ('hide'); } }); map.on ('pointermove', function(e) { if (e.dragging) { $ (element).popover ('destroy'); return; } var pixel = map.getEventPixel (e.originalEvent); var hit = map.hasFeatureAtPixel (pixel); map.getTarget ().style.cursor = hit ? 'pointer' : ''; }); 值。

答案 1 :(得分:1)

  1. 您不应使用默认构造函数创建Activity对象。阅读有关android核心组件的信息。
  2. 您可以将其存储为静态值。把它放在object里面,使它成为类变量。阅读有关实例/类变量的信息。