从Android片段开始活动

时间:2016-03-30 09:41:03

标签: java android android-fragments

我正在尝试开始录制音频的新活动。在片段“设置”中按下按钮(recordSong)时,将启动此活动。当我在手机上运行应用程序时,按下按钮,应用程序停止并显示消息“抱歉,应用已停止”(翻译自瑞典语)。

返回此logcat:

03-30 12:15:01.880 21959-21959/com.vkb.colium I/HWSERVICES: hwnative_get_component  comes...
03-30 12:15:01.880 21959-21959/com.vkb.colium W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41740c50)
03-30 12:15:03.705 21959-21959/com.vkb.colium I/Process: Sending signal. PID: 21959 SIG: 9

以下是“设置”类中onCreateView()方法的代码。注释行是我发现可能对我有帮助的代码,但给了我同样的错误:

 public class Settings extends Fragment {

    private Button recordSong;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.settings_fragment, container, false);

        recordSong = (Button)rootView.findViewById(R.id.recordSongBtn);
        recordSong.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("The record song button was clicked.");
                Intent intent = new Intent(getActivity(), RecordAudio.class);
                getActivity().startActivity(intent);
                //((MainActivity) getActivity()).startActivity(intent);
                //startActivity(intent);
                //Settings.this.startActivity(intent);
            }
        });

        return rootView;
    }
}

以下是RecordAudio类的代码:

public class RecordAudio extends Activity {

ImageButton record, stop, play, save;
EditText name;
private MediaRecorder audioRecorder;
private String outputFile;
Logic fsm = MainActivity.getFsm();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record_audio);

    record = (ImageButton) findViewById(R.id.recordBtn);
    stop = (ImageButton) findViewById(R.id.stopBtn);
    play = (ImageButton) findViewById(R.id.playBtn);

    stop.setEnabled(false);
    play.setEnabled(false);
    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp"; 

    audioRecorder = new MediaRecorder();
    audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    audioRecorder.setOutputFile(outputFile);

    record.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Turn off features of the ColiCot, so that it doesn't start when singing
            fsm.setPlayAudio(false);
            fsm.setPlayVideo(false);
            fsm.setRunMotor(false);

            try {
                audioRecorder.prepare();
                audioRecorder.start();
            }
            catch (IllegalStateException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            record.setEnabled(false);
            stop.setEnabled(true);

            Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_SHORT);
        }
    });

    stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Turn on features of the ColiCot
            fsm.setPlayAudio(true);
            fsm.setPlayVideo(true);
            fsm.setRunMotor(true);

            audioRecorder.stop();
            audioRecorder.release();
            audioRecorder = null;

            stop.setEnabled(false);
            play.setEnabled(true);

            Toast.makeText(getApplicationContext(), "Your song has been recorded", Toast.LENGTH_SHORT);
        }
    });

    play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MediaPlayer mp = new MediaPlayer();

            try {
                mp.setDataSource(outputFile);
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            try {
                mp.prepare();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            mp.start();
            Toast.makeText(getApplicationContext(), "Playing song", Toast.LENGTH_SHORT);
        }
    });

    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) { // Lægger in i egen fil på telefonen
            String path = FileHelper.getNextAudioFilePath();
            name = (EditText) findViewById(R.id.nameEditText);
            audioRecorder.setOutputFile(path + "/" + name);
        }
    });
}

@Override
protected void onDestroy() { // Deletes recorded sound from temporary file when activity is closed
    super.onDestroy();

    // Turn on features of the ColiCot
    fsm.setPlayAudio(true);
    fsm.setPlayVideo(true);
    fsm.setRunMotor(true);

    File recordedAudio = new File(outputFile);
    boolean success = recordedAudio.delete();
    if (!success) {
        System.out.println("The recorded file was not deleted");
    }
}

}

这是我的AndroidManifest:

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

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

<uses-feature android:name="android.hardware.usb.host" />

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.vkb.colium.media.RecordAudio"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="com.vkb.colium.media.RECORDAUDIO" />

            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
</application>

以下是记录音频的活动的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.vkb.colium.media.RecordAudio2">

<LinearLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/recordBtn"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/record_button"
        android:background="@android:color/background_light"
        android:layout_weight="50"
        android:gravity="right" />

    <ImageButton
        android:id="@+id/stopBtn"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/stop_button"
        android:background="@android:color/background_light"
        android:layout_weight="50"
        android:gravity="left" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_below="@+id/relativeLayout">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/playBtn"

    android:src="@drawable/play_button"
    android:background="@android:color/background_light"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Please enter a name for your song and click Save"
    android:id="@+id/name"
    android:layout_below="@+id/playBtn"
    android:layout_marginTop="45dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nameEditText" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/saveBtn"
        android:layout_below="@+id/textView"
        android:layout_marginTop="44dp" />

</LinearLayout>
</RelativeLayout>

设置片段的xml文件:

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

<TextView
    android:id="@+id/alarmText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Active Time 2 Minutes"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"/>

<SeekBar
    android:id="@+id/adjustTime"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/alarmText"
    android:max="90"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"/>

<CheckBox
    android:id="@+id/enableAudioCheckBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/adjustTime"
    android:text="Enable Audio"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"/>

<CheckBox
    android:id="@+id/enableVideoCheckBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/enableAudioCheckBox"
    android:text="Enable Video"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"/>

<CheckBox
    android:id="@+id/enableMotorheckBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/enableVideoCheckBox"
    android:text="Enable Motor"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"/>

<Button
    android:id="@+id/addSongBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/enableMotorheckBox"
    android:text="Add Song to Playlist"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"/>

<Button
    android:id="@+id/recordSongBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/addSongBtn"
    android:text="Record a song"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"/>

<Button
    android:id="@+id/playlistBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/recordSongBtn"
    android:text="Create a new playlist"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:onClick="playlistActivity"/>

<ListView
    android:id="@+id/playList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/addSongBtn"
    android:layout_alignRight="@+id/addSongBtn"
    android:layout_below="@+id/playlistBtn" >
</ListView>
</RelativeLayout>

0 个答案:

没有答案