Android按钮以图形布局显示,但不在设备上显示

时间:2014-06-09 08:28:44

标签: android

我一直试图在Android应用上显示两个按钮。但是,按钮不可见,整个页面都是空白。这是我的XML代码 -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.cameracapture.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/Capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="Capture"
        android:text="@string/capture" />
    <Button
        android:id="@+id/Upload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="Upload"
        android:text="@string/upload" />
    <ImageView 
        android:id="@+id/Display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/upload"/>

</LinearLayout>

当我在图形布局中查看上面的代码时,它会完美地显示按钮。所以,我想这意味着它不是match_parentwrap_content的问题。我甚至试图看看引入文本视图是否有所作为。再次,它完全显示在图形布局上,但在我的设备上,只有一个空白屏幕可见。 这可能是我的设备的问题,而不是代码吗?

P.S。我使用的设备是摩托罗拉Moto G,它有一个4.5英寸显示屏。

编辑:XML文件名为fragment_main。这是我的JAVA代码。     package com.example.cameracapture;

import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.provider.MediaStore;

public class MainActivity extends ActionBarActivity {

    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

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

    public void Capture(View view){

        // create Intent to take a picture and return control to the calling application
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
        imagesFolder.mkdirs();
        File image = new File(imagesFolder, "image_001.jpg");
        Uri uriSavedImage = Uri.fromFile(image);// create a file to save the image
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); // set the image file name

        // start the image capture Intent
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }

    public void Upload(View view){

        String path = Environment.getExternalStorageDirectory()+ "/DCIM/Camera/image_001.jpg"; 
        File imgFile = new File(path);
        if(imgFile.exists())
    {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  
            ImageView myImage = (ImageView) findViewById(R.id.Display);
            myImage.setImageBitmap(myBitmap);
    }
        else                    
            Toast.makeText(view.getContext(),"no IMAGE IS PRESENT'",Toast.LENGTH_SHORT).show();


    }


}

3 个答案:

答案 0 :(得分:0)

从您的回复中看起来您指的是错误的布局文件。因此,我假设你的XML是'fragment_main.xml,我把我的代码放在这里。这是我的MainActivity.java文件:

public class MainActivity extends ActionBarActivity {
static Button capture;
static Button upload;
static ImageView Display;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

public void Capture(View view){

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
    imagesFolder.mkdirs();
    File image = new File(imagesFolder, "image_001.jpg");
    Uri uriSavedImage = Uri.fromFile(image);// create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

public void Upload(View view){

    String path = Environment.getExternalStorageDirectory()+ "/DCIM/Camera/image_001.jpg"; 
    File imgFile = new File(path);
    if(imgFile.exists())
{
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  
        Display.setImageBitmap(myBitmap);
}
    else                    
        Toast.makeText(view.getContext(),"no IMAGE IS PRESENT'",Toast.LENGTH_SHORT).show();
}
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        capture = (Button) rootView.findViewById(R.id.Capture);
        upload = (Button) rootView.findViewById(R.id.Upload);
        Display = (ImageView) rootView.findViewById(R.id.Display);
        return rootView;
    }
}

}

及以下是屏幕截图,它在仿真器上的外观和完美执行情况。 enter image description here

答案 1 :(得分:0)

永远不会使用您的xml文件。

您的活动的setContentView()包含activity_main

检查您的布局activity_main - 在里面设置一个按钮并按原样运行代码,您将看到该按钮。

你想要的是设置fragment_main

一种方法是:

public class MainActivity extends Activity //or ActionBarActivity--considering it extends activity
{
        @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        if (savedInstanceState == null) 
        {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }
 
        public static class PlaceholderFragment extends Fragment 
    {
 
        public PlaceholderFragment() 
        {
        }
 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) 
        {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);//here is your fragment layout 
            return rootView;
        }
    }
}

进一步参考:
1. Example: sample default
2. Multi pane development: fragments
3. How to work with fragments - android

答案 2 :(得分:0)

在您的Activity类中 - 需要添加setContentView(R.layout.your_layout_xml) 其中“your_layout_xml”是您正在处理的xml文件。这可能会解决问题。 另外,请检查“tools:context =”com.example.cameracapture.MainActivity $ PlaceholderFragment“您在此处提供正确路径/上下文的行