java - 格式问题

标签 java android android-layout android-activity

我遇到了格式问题。这个简单的问题变得有点复杂;我收到错误,因为我的代码格式不正确。我尝试过,选择我的代码,然后右键单击 -> 源 -> 格式,但没有成功。

下面是完整的代码,

public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_profile_creation);
           save=(Button) findViewById(R.id.btnConfirm);
            String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
            if(!picturePath.equals(""))
            {
               ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
               imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }        

            Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
            buttonLoadImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Intent i = new Intent(
                            Intent.ACTION_PICK,
                             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
                }
            });        
            save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // Locate the image in res > 
                    Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
                    // Convert it to byte
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    // Compress image to lower quality scale 1 - 100
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                    Object image = null;
                    try {
                        String path = null;
                        image = readInFile(path);
                    }
                    catch(Exception e) { 
                        e.printStackTrace();
                    }

                    // Create the ParseFile
                    ParseFile file = new ParseFile("picturePath", (byte[]) image);
                    // Upload the image into Parse Cloud
                    file.saveInBackground();

                    // Create a New Class called "ImageUpload" in Parse
                    ParseObject imgupload = new ParseObject("Image");

                    // Create a column named "ImageName" and set the string
                    imgupload.put("Image", "picturePath");


                    // Create a column named "ImageFile" and insert the image
                    imgupload.put("ImageFile", file);

                    // Create the class and the columns
                    imgupload.saveInBackground();

                    // Show a simple toast message

                    SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar1); 
                    final TextView seekBarValue = (TextView)findViewById(R.id.seekbarvalue); 

                    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ 

                @Override 
                public void onProgressChanged(SeekBar seekBar, int progress, 
                  boolean fromUser) { 
                 // TODO Auto-generated method stub 
                 seekBarValue.setText(String.valueOf(progress)); 
                } 

                @Override 
                public void onStartTrackingTouch(SeekBar seekBar) { 
                 // TODO Auto-generated method stub 
                } 

                @Override 
                public void onStopTrackingTouch(SeekBar seekBar) { 
                 // TODO Auto-generated method stub 
                } 
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

    private byte[] readInFile(String path) throws IOException {
        // TODO Auto-generated method stub
        byte[] data = null;
        File file = new File(path);
        InputStream input_stream = new BufferedInputStream(new FileInputStream(
                file));
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        data = new byte[16384]; // 16K
        int bytes_read;
        while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, bytes_read);
        }
        input_stream.close();
        return buffer.toByteArray();

    }
}

特别是以下几行:

@Override 
public void onStopTrackingTouch(SeekBar seekBar) { 
 // TODO Auto-generated method stub 
} 
});
}}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

非常感谢您的帮助

最佳答案

仅查看您的代码,似乎您的左大括号和右大括号并不在所有地方都匹配。我将逐一检查所有这些内容,并确保左括号与正确的右括号匹配。例如。

在您所说的行末尾:

onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } }); }}

您尚未使用相应的“保存”按钮关闭 onclicklistener });而且你还有 }},这也是错误的格式。

根据您的 IDE(我知道 Android Studio 和 Eclipse 会这样做),如果您单击一个大括号,它会显示相应的右大括号的位置。除此之外,您的代码似乎没有任何问题。

编辑:请参阅下面的代码的固定版本:

public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);
        save = (Button) findViewById(R.id.btnConfirm);
        String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
        if (!picturePath.equals("")) {
            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }


        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Locate the image in res > 
                Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                Object image = null;
                try {
                    String path = null;
                    image = readInFile(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Create the ParseFile
                ParseFile file = new ParseFile("picturePath", (byte[]) image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("Image");

                // Create a column named "ImageName" and set the string
                imgupload.put("Image", "picturePath");


                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

                // Show a simple toast message

                SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
                final TextView seekBarValue = (TextView) findViewById(R.id.seekbarvalue);

                seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress,
                                                  boolean fromUser) {
                        // TODO Auto-generated method stub 
                        seekBarValue.setText(String.valueOf(progress));
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub 
                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub 
                    }
                });
            }
        });
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

关于java - 格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25027911/

相关文章:

java - 为什么 GridBagLayout 不创建第三列?

android - Kotlin + MVP - 意外覆盖

android - BottomNavigationView 自定义项目图像

android - 从布局看?

android - EditText 的子类看起来与 Android 4 上的普通 EditText 不同

java - 从java中的套接字断开连接

java - Pycharm 3.4 无法在 Yosemite 上运行

java - Android应用程序和序列化/反序列化效率?

android - 如何从 firebase 中检索选定的节点?

java - 使用循环在一个类中为另一个类的多个实例中的按钮添加 ActiveListener