简体   繁体   English

尝试在 Android 活动中执行简单命令时出错

[英]Error when trying to execute a simple command in Android activity

I am learning to read and write simple files using Android apps written in Java.我正在学习使用用 Java 编写的 Android 应用程序来读写简单的文件。 But I am not able to resolve this initial error!但我无法解决这个初始错误! I think I am getting this error because of this in place of context.我想我因为this而不是上下文而收到这个错误。 The app is compiling successfully but it is not opening after installing in my device.该应用程序编译成功,但在我的设备中安装后未打开。

I have not tried much but I am providing the code here.我没有尝试太多,但我在这里提供代码。 I am trying to accept a simple text through edit text view and then saving it to a file in Android using the button save.我试图通过编辑文本视图接受一个简单的文本,然后使用按钮保存将其保存到 Android 中的文件中。

package com.example.filemaketest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {
    String filename = "Testing-app-file.txt";
    File path = this.getFilesDir();

    //    File file = new File(path, filename);
//    FileOutputStream outputStream;
    public void save(View view) {
        EditText edit = (EditText) findViewById(R.id.infoText);
        String info = edit.getText().toString();
        Log.i("info", info);
        Toast.makeText(this, info + " button Pressed", Toast.LENGTH_LONG).show();
        Toast.makeText(this, " Saving", Toast.LENGTH_LONG).show();
//        try {
//            outputStream = openFileOutput(filename, this.MODE_PRIVATE);
//            outputStream.write(info.getBytes());
//            outputStream.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

The first thing that springs out to me is this line:我首先想到的是这条线:

public class MainActivity extends AppCompatActivity {
    String filename ="Testing-app-file.txt";
    File path= this.getFilesDir();           // <<< This line

You are defining path inline within the class definition, which is equivalent to setting it within the MainActivity() constructor.您正在类定义中定义内联path ,这相当于在MainActivity()构造函数中设置它。 This is before the Activity Lifecycle has begun, therefore this as a Context does not exist yet.这是在Activity Lifecycle开始之前,因此this作为Context尚不存在。 You will need to define path later on in the activity lifecycle, for example during onCreate() :您将需要在活动生命周期稍后定义path ,例如在onCreate()期间:

public class MainActivity extends AppCompatActivity {
    String filename ="Testing-app-file.txt";
    File path;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        path = this.getFilesDir();
    }

    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM