在整个 activity 中重复使用 String 或 Int?

Re-Use a String or Int througout the whole activity?

我没有意识到我在 MainAcitivity 中一直在使用一个 int 和一个 string。

现在想想,感觉他们要泄漏内存了

代码:

public class MainActivity extends Activity {

private Button deleteButton;
private String imagePath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        deleteButton = (Button) findViewById(R.id.delete_button);
        //===== SHARED_PREFERENCES ==============================================================
        SharedPrefs = this.getSharedPreferences("SHARED_PREFS", Context.MODE_PRIVATE);
        prefs_editor = SharedPrefs.edit();

         try {
              imagePath = SharedPrefs.getString("ImagePath", "");
            }
        catch (Exception e) {  }    
      }

     .....

     .....

            deleteButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    File file = new File(imagePath);
                    .....

                }
            }); 

            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    imageLoader.loadImage(imagePath, new SimpleImageLoadingListener() {
                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            mImageView.setImageBitmap(loadedImage);                                 
                        }
                    });
                }

public void LoadNextImagePath() {
    File file = new File(LoadUserSavedPathString);
    if(file.isDirectory()) {
        listFile = new File(LoadUserSavedPathString).listFiles();                       
        imagePath = listFile[LastItemPosition-1].getAbsolutePath();
    }
}

}

这是坏内存泄漏吗?

非常感谢

它不会造成任何影响。垃圾收集会处理它。 私有字段促进封装

使用 private 是普遍接受的约定,除非您需要向其他人公开字段或方法 类。养成这种习惯会在漫长的 运行.

中为您省去很多痛苦

但是,public 字段或方法本身并没有任何错误。它对垃圾收集没有影响。