如何将视图添加到Android框架布局

发布于 2024-12-15 08:09:19 字数 2154 浏览 4 评论 0原文

我正在尝试开发一个简单的相机应用程序。 我使用了 Google 提供的示例 CameraPreview 代码。 甚至下面的代码也取自 Android 开发者网站上的文档。 但是添加视图 mPreview 时出现问题。 它在以下 2 种情况下失败 - 它说应用程序意外停止,请重试 - 也没有日志:

i)preview.addView(mPreview)和

ii)setContentView(mPreview)

我尝试了选项 ii)只是为了消除以下可能性CameraPreview 代码有问题。

但选项 i) 即使对于捕获按钮也不起作用。 所以我想知道 addView 有什么问题。

逛了很多论坛,都没看到这个问题。我也考虑过使用 LayoutInflater 但我不知道它在这里如何应用。

public class CameraTrialActivity extends Activity implements OnClickListener{

    /** Called when the activity is first created. */
 private Camera mCamera;    
 private CameraPreview mPreview; 
 private final String TAG = "Camera Preview";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Create an instance of Camera       
        mCamera = getCameraInstance();        
        // Create our Preview view and set it as the content of our activity.      
        mPreview = new CameraPreview(this, mCamera);      
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);       
        preview.addView(mPreview);

        Button captureButton = (Button)findViewById(R.id.button_capture);
        //preview.addView(captureButton);
        //captureButton.setOnClickListener(this);
        //setContentView(R.layout.main);
        //setContentView(mPreview);
    }
}

//XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
     android:orientation="horizontal"    
     android:layout_width="fill_parent"    
     android:layout_height="fill_parent"   
>  
 <FrameLayout    
     android:id="@+id/camera_preview"    
     android:layout_width="fill_parent"    
     android:layout_height="fill_parent"    
     android:layout_weight="1"    />  

    <Button    
        android:id="@+id/button_capture"    
        android:text="Capture"    
        android:layout_width="wrap_content"    
        android:layout_height="wrap_content"    
        android:layout_gravity="center"
    />
</LinearLayout>

I'm trying to get a simple camera app up.
I have used the sample CameraPreview code provided by Google.
And even the code below is taken from the documentation on the android developers site.
But there's a problem with adding the view mPreview.
It fails in the following 2 conditions-it says application stopped unexpectedely, please try again-there are no logs either:

i) preview.addView(mPreview) and

ii)setContentView(mPreview)

I tried the option ii) just to eliminate possibility of there being something wrong with the CameraPreview code.

But option i) does not work even for the Capture Button.
So I was wondering what is wrong with addView.

I did go through a lot of forums, haven't seen this problem. I also considered using LayoutInflater but I don't see how it applies here.

public class CameraTrialActivity extends Activity implements OnClickListener{

    /** Called when the activity is first created. */
 private Camera mCamera;    
 private CameraPreview mPreview; 
 private final String TAG = "Camera Preview";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Create an instance of Camera       
        mCamera = getCameraInstance();        
        // Create our Preview view and set it as the content of our activity.      
        mPreview = new CameraPreview(this, mCamera);      
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);       
        preview.addView(mPreview);

        Button captureButton = (Button)findViewById(R.id.button_capture);
        //preview.addView(captureButton);
        //captureButton.setOnClickListener(this);
        //setContentView(R.layout.main);
        //setContentView(mPreview);
    }
}

//XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
     android:orientation="horizontal"    
     android:layout_width="fill_parent"    
     android:layout_height="fill_parent"   
>  
 <FrameLayout    
     android:id="@+id/camera_preview"    
     android:layout_width="fill_parent"    
     android:layout_height="fill_parent"    
     android:layout_weight="1"    />  

    <Button    
        android:id="@+id/button_capture"    
        android:text="Capture"    
        android:layout_width="wrap_content"    
        android:layout_height="wrap_content"    
        android:layout_gravity="center"
    />
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文