java - 运行应用程序时布局不显示

标签 java android android-studio layout

当我运行该应用程序时,它没有显示任何布局,只是一个空白页面 这是我的 Java 代码:

public class MainActivity extends AppCompatActivity {
private boolean zoomOut = false;
int REQUEST_CAMERA = 0, SELECT_FILE = 1;
Button btnSelect;


LinearLayout root ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
    root = (LinearLayout) findViewById(R.id.ll);


    btnSelect.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            selectImage();
        }
    });

    ImageView ivImage = (ImageView) findViewById(R.id.ivImage);
}

private void selectImage() {
    final CharSequence[] items = { "Take Photo", "Choose from Library",
            "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, REQUEST_CAMERA);
            } else if (items[item].equals("Choose from Library")) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/* video/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

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

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    Bitmap resized = Bitmap.createScaledBitmap(thumbnail, 800, 150, true);
    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ImageView ivImage = new ImageView(this);
    GradientDrawable gd = new GradientDrawable();
    gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
    gd.setCornerRadius(5);
    gd.setStroke(1, 0xFF000000);

    ivImage.setBackground(gd);
    Point point = null;
    getWindowManager().getDefaultDisplay().getSize(point);
    int width = point.x;
    int height = point.y;

    ivImage.setMinimumWidth(width);
    ivImage.setMinimumHeight(height);

    ivImage.setMaxWidth(width);
    ivImage.setMaxHeight(height);
    ivImage.getLayoutParams().width = 20;
    ivImage.getLayoutParams().height = 20;
    ivImage.setLayoutParams(new ActionBar.LayoutParams(
            GridLayout.LayoutParams.MATCH_PARENT,
            GridLayout.LayoutParams.WRAP_CONTENT));
    ivImage.setImageBitmap(thumbnail);
    root.addView(ivImage);

 setContentView(root);
 ivImage.setImageBitmap(thumbnail);
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
    Uri selectedImageUri = data.getData();
    String[] projection = { MediaStore.MediaColumns.DATA };
    Cursor cursor = managedQuery(selectedImageUri, projection, null, null,
            null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    cursor.moveToFirst();

    String selectedImagePath = cursor.getString(column_index);

    Bitmap bm;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(selectedImagePath, options);
    final int REQUIRED_SIZE = 200;
    int scale = 1;
    while (options.outWidth / scale / 2 >= REQUIRED_SIZE
            && options.outHeight / scale / 2 >= REQUIRED_SIZE)
        scale *= 2;
    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(selectedImagePath, options);
    final ImageView ivImage = new ImageView(this);
    ivImage .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(zoomOut) {
                Toast.makeText(getApplicationContext(), "NORMAL SIZE!", Toast.LENGTH_LONG).show();
                ivImage.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ivImage.setAdjustViewBounds(true);
                zoomOut =false;
            }else{
                Toast.makeText(getApplicationContext(), "FULLSCREEN!", Toast.LENGTH_LONG).show();
                ivImage.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                ivImage.setScaleType(ImageView.ScaleType.FIT_XY);
                zoomOut = true;
            }
        }
    });
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();

    ivImage.setMinimumWidth(width);
    ivImage.setMinimumHeight(height);

    ivImage.setMaxWidth(width);
    ivImage.setMaxHeight(height);
    ivImage.setLayoutParams(new ActionBar.LayoutParams(
            1000,
            1000));
    ivImage.setImageBitmap(bm);
    root.addView(ivImage);
    setContentView(root);
    ivImage.setImageBitmap(bm);

}

}

这是我的 xml 代码:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="5dp" >

    <Button
        android:id="@+id/btnSelectPhoto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/SelectPhoto" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

我被告知这是因为 onCreate 没有正确放置,但我决定它仍然没有显示任何内容。

最佳答案

请检查您的 .Xml 文件名是否匹配

setContentView(R.layout.activity_main);

关于java - 运行应用程序时布局不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35255897/

相关文章:

java - 无法在 Jboss AS 7 上定义 oracle 数据源

java - 向上导航键 : NullPointerException Java Android

java - 如何修复Java中的 'Could not write standard input to Gradle Test Executor 1'错误

java - 模拟基于反射的调用

java - readLine()怎么会出现空指针异常呢?

android - 具有可变项目高度的水平 RecyclerView 未正确包装

android-studio - 在查找结果中忽略 R.java 文件

java - 如何以排序的方式检索文档的值?

android - 当用户按下键盘上的按钮时,在字母键盘和数字键盘之间切换

android - 使Gradle更快的不同方法