2011-06-04 139 views
0

我有我的android應用程序的相機預覽,但是當我添加一個'id'到xml中的T​​extView然後應用程序不再運行。你不允許有一個帶有ID的文本視圖?我需要這個,因爲我希望在預覽相機時在屏幕上更改文字?安卓相機預覽與textview

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<SurfaceView android:id="@+id/camerapreview" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 
</LinearLayout> 

control.xml

<?xml version="1.0" encoding="utf-8"?> 

    <ImageView android:src="@drawable/icon" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_gravity="center" 
     android:paddingTop="120dp" /> 
    <TextView android:layout_width="wrap_content" android:id="@+id/mind" 
     android:layout_height="wrap_content" android:text="First Text" 
     android:layout_gravity="center" android:paddingTop="50dp" 
     android:textSize="30dp" android:textStyle="bold" android:textColor="#FFFFFF" /> 
    <Button android:id="@+id/takepicture" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="Save Image" 
     android:layout_gravity="center" android:paddingTop="10dp"/> 
</LinearLayout> 

main.java:

package com.example.androidcamera; 

import java.io.IOException; 
import android.app.Activity; 
import android.content.pm.ActivityInfo; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.PixelFormat; 
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback; 
import android.hardware.Camera.ShutterCallback; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.TextView; 

public class main extends Activity implements SurfaceHolder.Callback { 

Camera camera; 
SurfaceView surfaceView; 
SurfaceHolder surfaceHolder; 
boolean previewing = false; 
LayoutInflater controlInflater = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    getWindow().setFormat(PixelFormat.UNKNOWN); 
    surfaceView = (SurfaceView) findViewById(R.id.camerapreview); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    controlInflater = LayoutInflater.from(getBaseContext()); 
    View viewControl = controlInflater.inflate(R.layout.control, null); 
    LayoutParams layoutParamsControl = new LayoutParams(
      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    this.addContentView(viewControl, layoutParamsControl); 

    TextView textGenerateDesc = (TextView) findViewById(R.id.mind); 
    textGenerateDesc.setText("hi"); 

    Button buttonTakePicture = (Button) findViewById(R.id.takepicture); 
    buttonTakePicture.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      camera.takePicture(myShutterCallback, myPictureCallback_RAW, 
        myPictureCallback_JPG); 
     } 
    }); 
} 
+0

添加的代碼。基本上,它只適用於textview中沒有id的情況。 – karlstackoverflow 2011-06-04 00:22:52

+0

據我所知,它應該可以正常工作,除非android不喜歡在照相機上使用textviews。 – karlstackoverflow 2011-06-04 00:43:22

+0

你得到的錯誤是什麼? – MByD 2011-06-04 00:44:55

回答

1

,我可以在你給textview ID爲「的TextView的XML見「在代碼中,你可以通過R.id.mind

您是否更改過代碼或您的代碼?

+0

我已經改變了,我只是編輯它爲stackoverflow。在我的他們是一樣的。 – karlstackoverflow 2011-06-04 00:55:58

+0

你試過做'viewControl.getChildAt(1)'嗎?並拿出ID:D,這是一個有點愚蠢,但嘗試它 – Cata 2011-06-04 01:03:48

+0

這很奇怪。我重新啓動了手機,現在它工作。它接受id,甚至改變text.wtf。 – karlstackoverflow 2011-06-04 01:16:29

0

你只是錯過了 '的LinearLayout'

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

休息的開始標記相同

<ImageView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:paddingTop="120dp" 
/> 
<TextView 
android:id="@+id/mind" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:paddingTop="50dp" 
android:text="First Text" 
android:textColor="#FFFFFF" 
android:textSize="30dp" 
android:textStyle="bold" /> 

<Button 
android:id="@+id/takepicture" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:paddingTop="10dp" 
android:text="Save Image" /> 

</LinearLayout>