1

我有一个 RemoteCar 应用程序,可以调节空调、启动引擎等。在我的 MainActivity 中有一个 FuelBar 和一个 SeekBar(AC)。我想保存这些实例以供下次运行。例如,当我将空调设置为 20° 并关闭应用程序时,我希望空调打开 20°,就像我离开它的方式一样。但不知何故,我把编码搞砸了,以至于它甚至不再启动,你能解释一下我错过了什么吗?

在 C++ 中,我可以轻松地打开一个文件并立即关闭它,但是“close()”方法不太有效。我也很欣赏与此主题相关的任何信息,我可以在其中阅读。

保存方法“saveInfo”并在 onStop 中我希望它保存当前状态。

package com.example.schwarzerritter.remotecv02;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import static com.example.schwarzerritter.remotecv02.R.id.engB;
import static com.example.schwarzerritter.remotecv02.R.id.fuelProgressBar;
import static com.example.schwarzerritter.remotecv02.R.id.refuelB;
import static com.example.schwarzerritter.remotecv02.R.id.seekBar;

public class MainActivity extends AppCompatActivity {
    public ProgressBar fuelBar;
    public Button lockButton;
    public Button engButton;
    public Button refuelButton;
    public Button locationButton;
    public SeekBar seekBarButton;
    public TextView seekText;
    int incFuel = 0;


    public void onClick(View v) {
        switch (v.getId()) {
            case engB:
                if (engButton.getText() == "ENGINE RUNNING") {
                    engButton.setText("START ENGINE");
                } else {
                    if (fuelBar.getProgress() > 0) {
                        Toast.makeText(MainActivity.this, "starting engine..", Toast.LENGTH_SHORT).show();
                        engButton.setText("ENGINE RUNNING");
                        if (fuelBar.getProgress() >= 10) {
                            incFuel = fuelBar.getProgress();
                            incFuel -= 10;
                            fuelBar.setProgress(incFuel);
                            if (fuelBar.getProgress() < 100)
                                refuelButton.setText("REFUEL");
                        }
                    } else if (fuelBar.getProgress() == 0) {
                        Toast.makeText(MainActivity.this, "no fuel", Toast.LENGTH_SHORT).show();
                        engButton.setText("EMPTY GASTANK");
                    } else
                        engButton.setText("START ENGINE");
                }
                break;
            case refuelB:
                if (fuelBar.getProgress() == 0) {
                    engButton.setText("START ENGINE");
                    incFuel = fuelBar.getProgress();
                    incFuel += 10;
                    fuelBar.setProgress(incFuel);
                } else if (fuelBar.getProgress() < 100) {
                    incFuel = fuelBar.getProgress();
                    incFuel += 10;
                    fuelBar.setProgress(incFuel);
                } else {
                    Toast.makeText(MainActivity.this, "tank is full", Toast.LENGTH_SHORT).show();
                    refuelButton.setText("FULL");
                }
                break;
        }
    }

    public void seek_bar() {
        seekBarButton = (SeekBar) findViewById(R.id.seekBar);
        seekText = (TextView) findViewById(R.id.seekText);
        seekText.setText("AC : " + (seekBarButton.getProgress() + 18) + "°");
        seekBarButton.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int progressNum;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                progressNum = progress;
                seekText.setText("AC : " + (seekBarButton.getProgress() + 18) + "°");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                seekText.setText("AC : " + (seekBarButton.getProgress() + 18) + "°");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                seekText.setText("AC : " + (seekBarButton.getProgress() + 18) + "°");
            }
        });
    }

    public void saveInfo() {
        SharedPreferences sharedPref = getSharedPreferences("conditions", 0);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("seekBar", seekText.getText().toString());
        editor.putInt("refuelBar", fuelBar.getProgress());
        editor.commit();
    }

    public void lockPage() {
        lockButton = (Button) findViewById(R.id.lockB);
        lockButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent lockPage = new Intent(MainActivity.this, lockDoor.class);
                startActivity(lockPage);
            }
        });
    }

    public void locationPage() {
        locationButton = (Button) findViewById(R.id.locationB);
        locationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent locationPage = new Intent(MainActivity.this, location.class);
                startActivity(locationPage);
            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        saveInfo();//read files
        locationButton = (Button) findViewById(R.id.locationB);
        lockButton = (Button) findViewById(R.id.lockB);
        engButton = (Button) findViewById(R.id.engB);
        refuelButton = (Button) findViewById(R.id.refuelB);
        fuelBar = (ProgressBar) findViewById(R.id.fuelProgressBar);
        fuelBar.setMax(100);
        fuelBar.setProgress(30);
        refuelButton.setText(R.string.refuelB);
        lockButton.setText(R.string.lockB);
        locationButton.setText(R.string.locationB);
        engButton.setText(R.string.engB);
        seekBarButton = (SeekBar) findViewById(R.id.seekBar);
        seekText = (TextView) findViewById(R.id.seekText);
        seek_bar();
        lockPage();
        locationPage();


    }

    protected void onStop(){
        super.onStop();

        //Open SharedPref
        SharedPreferences sharedPref = getSharedPreferences("conditions", 0);
        //Edit
        SharedPreferences.Editor editor = sharedPref.edit();
        //Write file
        editor.putString("seekBar", seekText.getText().toString());
        editor.putInt("refuelBar", fuelBar.getProgress());
        //save
        editor.commit();
        //editor.close; Error: can't resolve method/symbol close
    }

}

日志猫:

    05-24 08:00:54.870 2026-2040/com.google.process.gapps W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gsf/databases/gservices.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:00:55.040 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/networkstatistics.sqlite' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:28:52.605 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:28:52.605 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/help_responses.db.18' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:28:52.606 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/metrics.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:29:04.497 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:29:04.499 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/help_responses.db.18' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:29:04.499 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/metrics.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:30:52.533 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/metrics.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:30:52.535 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/help_responses.db.18' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:30:52.541 2146-2157/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
05-24 08:31:07.294 1796-1796/com.google.android.googlequicksearchbox W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
05-24 08:31:51.101 3983-4041/com.google.android.gms I/FA-SVC: App measurement is starting up, version: 10298
05-24 08:31:51.190 3983-4199/com.google.android.gms I/FA-SVC: This instance being marked as an uploader
4

0 回答 0