【问题标题】:Android Button shows in graphical layout but not on deviceAndroid Button 以图形布局显示,但不在设备上
【发布时间】:2014-06-09 17:06:44
【问题描述】:

我一直在尝试在 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 的问题。我什至试图看看引入文本视图是否会有所作为。再次,它完美地显示在图形布局上,但在我的设备上,只有一个空白屏幕可见。 可能是我的设备问题,而不是代码问题?

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

编辑:XML 文件名为 fragment_main。这是我的 JAVA 代码。 包 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();


    }


}

【问题讨论】:

  • 也许您正在以编程方式更改可见性?
  • 在您引用此布局的位置发布活动代码。另外,请说明您的布局 xml 的名称
  • 检查您是否正在更改 onCreate 方法中按钮的可见性。
  • 除非您发布您的 Java 文件,否则 SO 无法帮助您。就您的 XML 而言,它看起来不错。
  • 这个 layout file 的名称是什么。我认为这个布局在放置架下膨胀了......所以可能就是这个问题......

标签: android


【解决方案1】:

您的 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】:

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

【讨论】:

    【解决方案3】:

    从您的回复看来,您指的是错误的布局文件。因此,我将代码放在这里,假设您的 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;
        }
    }
    

    }

    下面是它在模拟器上的外观和完美执行的屏幕截图。

    【讨论】:

    • 是的,大概就是这样。我没有使用 Layout Inflater。一个小问题仍然存在,如果你能忍受我的话。在包括 getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();我收到错误声明 FragmentTransaction 类型中的方法 add(int, Fragment) 不适用于参数 (int, MainActivity.PlaceholderFragment)。我该如何纠正?
    • 酷,请接受它作为答案并投票。谢谢你,欢迎来到 Android 编程。
    • 我无法投票。我需要至少 15 声望。对不起。
    • 但是你可以同时接受它作为答案。以后当你超过 15 岁时,你可以投票 :)
    猜你喜欢
    • 2021-07-07
    • 2013-07-21
    • 2013-08-17
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多