当我在屏幕上运行时是白色/空白

时间:2014-04-27 15:56:50

标签: android eclipse

好的,当我运行VideoRecorder活动时,我的应用程序是空白的,似乎无法在我的代码中找到问题。 VideoRecorder活动中没有错误,但是当我在手机上运行时,它仍然只显示一个白色的空白屏幕。

logcat说什么

04-27 12:50:22.533: W/System.err(31693):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
04-27 12:50:22.533: W/System.err(31693):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
04-27 12:50:22.533: W/System.err(31693):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
04-27 12:50:22.533: W/System.err(31693):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
04-27 12:50:22.533: W/System.err(31693):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
04-27 12:50:22.533: W/System.err(31693):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 12:50:24.906: W/System.err(31693):    ... 20 more
04-27 12:50:24.906: W/System.err(31693):    at java.lang.reflect.Method.invoke(Method.java:511)
04-27 12:50:24.906: W/System.err(31693):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-27 12:50:24.906: W/System.err(31693):    at dalvik.system.NativeStart.main(Native Method)
04-27 12:50:24.906: W/System.err(31693):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-27 12:50:24.906: W/System.err(31693):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-27 12:50:24.906: W/System.err(31693):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)

以下是活动的代码

import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

public class VideoRecorder extends Activity {

private String audiofile;
private Button backbtn;
private Button btn_save;
private Button btnSound;
private ToggleButton cameraSwitch;
private int cameraType = 0;



private View.OnClickListener click = new View.OnClickListener() {

    public void onClick(View paramAnonymousView) {
        switch (paramAnonymousView.getId())
        {
        case R.layout.powered_by:
        case R.id.layout:
        default:
            return;
        case R.id.btn_save:
            VideoRecorder.this.chooseSaveDialog();
            return;
        case R.id.backbtn:
            VideoRecorder.this.onBackPressed();
            return;
        }

    }
};
Context contex;
int count = 0;
String fileName;
String filePath;
boolean flag = false;
boolean frontCam;
SurfaceHolder holder;
LayoutInflater inflater;
boolean isPlaySound = false;
boolean isRecording = true;
private Preview mPreview;
private MediaPlayer mp;
private Camera myCamera;
PackageManager pm;
boolean rearCam;
private MediaRecorder recorder;
RelativeLayout rootLayout;
boolean startedRecording = false;
boolean stoppedRecording = false;
long timestamp;

private Camera getCameraInstance(int paramInt)
{
    try
    {
        Camera localCamera = Camera.open(paramInt);
        return localCamera;
    }
    catch (Exception localException) {}
    return null;
}

private void initCameraView(MediaRecorder paramMediaRecorder)
{
    try 
    {
        if (this.mPreview != null) {
            this.rootLayout.removeView(this.mPreview);
        }
        this.mPreview = ((Preview)this.inflater.inflate(R.layout.camera_layout, null));
        RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(-1, -1);
        this.mPreview.setvActivity(this);
        this.mPreview.setTempRecorder(paramMediaRecorder);
        this.mPreview.setFilePath(this.filePath);
        this.holder = this.mPreview.getHolder();
        this.rootLayout.addView(this.mPreview, localLayoutParams);
        return;
    }
    catch (Exception localException)
    {
        for (;;)
        {
            localException.printStackTrace();
        }
    }
}

private void initData()
{
    this.audiofile = getIntent().getStringExtra(audiofile);
    if (this.audiofile == null) {}
}

private void initView()
{
    this.btn_save = ((Button)findViewById(R.id.btn_save));
    this.backbtn = ((Button)findViewById(R.id.backbtn));
    this.btnSound = ((Button)findViewById(R.id.btnSound));
    this.cameraSwitch = ((ToggleButton)findViewById(R.id.cameraSwitch));
    this.rootLayout = ((RelativeLayout)findViewById(R.id.layout));
    if (!this.frontCam) {
        this.cameraSwitch.setVisibility(8);
    }
}

private void mediaRecorderSetup()
{
    this.myCamera = getCameraInstance(this.cameraType);
    this.recorder = new MediaRecorder();
    this.myCamera.unlock();
    this.recorder.setCamera(this.myCamera);
    this.recorder.setAudioSource(1);
    this.recorder.setVideoSource(1);
    this.recorder.setOutputFormat(0);
    this.recorder.setAudioEncoder(0);
    this.recorder.setVideoEncoder(0);
    this.recorder.setOutputFile(this.filePath);
    initCameraView(this.recorder);
}

private void releaseCamera()
{
    if (this.myCamera != null)
    {
      this.myCamera.release();
      this.myCamera = null;
    }
  }

  private void releaseMediaRecorder()
  {
    if (this.recorder != null)
    {
      this.recorder.reset();
      this.recorder.release();
      this.recorder = null;
      this.myCamera.lock();
    }
  }

private void setListener()
{
    this.btn_save.setOnClickListener(this.click);
    this.backbtn.setOnClickListener(this.click);
    this.btnSound.setOnClickListener(this.click);
    this.cameraSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton paramAnonymousCompoundButton, boolean paramAnonymousBoolean) {
            VideoRecorder localVideoRecorder = VideoRecorder.this;
            localVideoRecorder.count = (1 + localVideoRecorder.count);
            if (VideoRecorder.this.count % 2 == 0) {}
            for (VideoRecorder.this.cameraType = 0;;)
            {
                VideoRecorder.this.releaseMediaRecorder();
                VideoRecorder.this.releaseCamera();
                VideoRecorder.this.timestamp = new Date().getTime();
                VideoRecorder.this.filePath = (Environment.getExternalStorageDirectory() + "/" + String.valueOf(VideoRecorder.this.timestamp) + ".3gpp");
                VideoRecorder.this.mediaRecorderSetup();
                return;
            }
        }
    });
}

private void startRecording()
{
    if (this.recorder != null) {
        this.recorder.start();
    }
}

private void stopRecording()
{
    if (this.recorder != null)
    {
        this.recorder.stop();
        this.recorder.release();
        this.recorder = null;
    }
}

public void chooseSaveDialog()
{
    AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
    localBuilder.setTitle("Save.");
    localBuilder.setMessage("Press OK to save");
    localBuilder.setNegativeButton("ok", new DialogInterface.OnClickListener()    {

        @Override
        public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
            paramAnonymousDialogInterface.dismiss();
            VideoRecorder.this.releaseMediaRecorder();
            VideoRecorder.this.releaseCamera();
            VideoRecorder.this.stopRecording();
            VideoRecorder.this.StopAudio();
            Intent localIntent = new Intent();
            localIntent.putExtra("result", VideoRecorder.this.filePath);
            VideoRecorder.this.setResult(-1, localIntent);
            VideoRecorder.this.finish();
        }
    });
localBuilder.create().show();
}

public void initRecord()
{
    startRecording();
}

public void onBackPressed()
{
    StopAudio();
    releaseMediaRecorder();
    releaseCamera();
    super.onBackPressed();
}

protected void onCreate(Bundle paramBundle)
{
    super.onCreate(paramBundle);
    setContentView(R.layout.recordvideo_layout);
            this.pm = getPackageManager();
    this.frontCam = this.pm.hasSystemFeature("android.hardware.camera.front");
    this.rearCam = this.pm.hasSystemFeature("android.hardware.camera");
    this.inflater = LayoutInflater.from(this);
    initView();
    initData();
    setListener();
    this.timestamp = new Date().getTime();
    this.filePath = (Environment.getExternalStorageDirectory() + "/" + String.valueOf(this.timestamp) + ".3gpp");
    mediaRecorderSetup();
}

public void playAudio()
{
    if (this.mp == null) {
        this.mp = new MediaPlayer();
    }
    try
    {
        AssetFileDescriptor localAssetFileDescriptor = getAssets().openFd(this.audiofile);
        this.mp.setDataSource(localAssetFileDescriptor.getFileDescriptor(),   localAssetFileDescriptor.getStartOffset(), localAssetFileDescriptor.getLength());
        localAssetFileDescriptor.close();
        this.mp.prepare();
        this.mp.setLooping(true);
        this.mp.start();
        this.mp.setVolume(10.0F, 10.0F);
        return;
    }
    catch (Exception localException) {}
}

protected void play_pauseAudio()
{
    if (this.isPlaySound) {}
    for (boolean bool = false;;)
    {
        this.isPlaySound = bool;
        if (!this.isPlaySound) {
            break;
        }
        this.btnSound.setText("Pause Sound");
        playAudio();
        return;
    }
    this.btnSound.setText("Play Sound");
    StopAudio(); 
}

public void StopAudio()
{
    if (this.mp != null)
    {
        this.mp.stop();
        this.mp.release();
        this.mp = null;
    }
}

}

此处还有manifest.xml文件的代码。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.soloinc.meip"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity 
        android:name="com.soloinc.meip.Webview" 
        android:screenOrientation="portrait" 
        android:windowSoftInputMode="stateAlwaysHidden" />


    <intent-filter>
        <!--Viewer filter-->
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>

      <!-- DEFAULT LAUNCHER filter-->
    <activity android:label="@string/app_name"   android:name="com.soloinc.meip.VideoRecorder" 
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!--custom filter-->
    <intent-filter>

    </intent-filter>

</application>
 </manifest>

继承布局的代码

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

 <RelativeLayout 
     android:id="@+id/headerlayout" 
     android:background="@drawable/header" 
     android:layout_width="fill_parent" 
     android:layout_height="50.0dip" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true">

    <Button 
        android:id="@+id/backbtn" 
        android:background="@drawable/backbutton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="14.0dip" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        style="?android:attr/buttonStyleSmall" />

    <TextView 
        android:textAppearance="?android:textAppearanceMedium" 
        android:textStyle="bold"
        android:textColor="@color/lime_green" 
        android:id="@+id/textView1userlike" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" 
        android:layout_centerHorizontal="true" 
        android:layout_centerVertical="true" />

    <Button 
        android:textStyle="bold" 
        android:id="@+id/btn_save" 
        android:background="@drawable/loginbtn" 
        android:layout_width="70.0dip" 
        android:layout_height="wrap_content" 
        android:text="@string/save" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentBottom="true" />
</RelativeLayout>

 <RelativeLayout 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/headerlayout" />

 <RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="50.0dip" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentBottom="true">

    <Button 
        android:textStyle="bold" 
        android:id="@+id/btnSound" 
        android:background="@drawable/loginbtn" 
        android:layout_width="110.0dip" 
        android:layout_height="wrap_content" 
        android:text="@string/sound"
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" />

    <ToggleButton 
        android:textStyle="bold" 
        android:id="@+id/cameraSwitch" 
        android:background="@drawable/rect_nonselected" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textOn="Rear Camera" 
        android:textOff="Front Camera" 
        android:layout_alignParentTop="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentBottom="true" />
</RelativeLayout>

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

您需要在setContentView(R.layout.whatever_layout)行之后直接拨打onCreate requestWindowFeature行(通常在致超级电话后直接)

答案 1 :(得分:0)

在您的oncreate方法上添加@Override。像这样

@Override //======>> This line
protected void onCreate(Bundle paramBundle)
{
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    this.pm = getPackageManager();
    this.frontCam = this.pm.hasSystemFeature("android.hardware.camera.front");
    this.rearCam = this.pm.hasSystemFeature("android.hardware.camera");
    setContentView(R.layout.recordvideo_layout);
    this.inflater = LayoutInflater.from(this);
    initView();
    initData();
    setListener();
    this.timestamp = new Date().getTime();
    this.filePath = (Environment.getExternalStorageDirectory() + "/" + String.valueOf(this.timestamp) + ".3gpp");
    mediaRecorderSetup();
}

答案 2 :(得分:0)

您的问题是您在调用setcontentview()后调用函数requestwindowfeature()。必须始终首先调用requestwindowfeature()。

相关问题