user3538235
user3538235

Reputation: 2009

Form program generated layout to XML layout in android

Recently I studied code from android example, it is record audio example, and there are two custom classes extend button eg.RecordButton and PlayButton

The code is like:

public class AudioRecordTest extends Activity
{
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;

    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;

    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;

    class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            setOnClickListener(clicker);
        }
    }



    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        mPlayButton = new PlayButton(this);
        ll.addView(mPlayButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        setContentView(ll);
    }


}

And I would like to change it to XML , however, it return the error of class not found

 Suppressed: java.lang.ClassNotFoundException: com.example.user.test.MainActivity.RecordButton

Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

And in XML designer view, it also has a

 java.lang.LinkageError (loader attempt a duplicate class definition for name : RecordButton)

Here is the XML I try to structure:

<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:gravity="center"
    tools:context=".MainActivity">

    <com.example.user.test.MainActivity.RecordButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rec_btn" />

    <com.example.user.test.MainActivity.PlayButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/play_btn" />

</LinearLayout>

is there something that I am missing, I checked the package name is correct. How do I fix this?

Update: manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.test" >

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

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

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

Upvotes: 0

Views: 163

Answers (1)

Virthuss
Virthuss

Reputation: 3213

First, try the classical clean/rebuild project. It will ensure that there is really a problem with your code and not with the debugger

Then, I recommend you to declare your two classes in different files, it will be more readable, also problems can happen more frequently while using inner classes.

I see that your problem is linked to the one in this post: extend button android, xml layout, have you try the given answer?

I understand "(included into another class)" as you have an inner class RecordButton.

Assuming your package is AudioRecordTest.test (which would be a very bad name choice) and your RecordButton class is an inner class of AudioRecord.class, you need to use:

BTW: any particular reason you create it as an inner class instead of having it separate?

You are using an inner class for your buttons. Then the way you have to use it is different. Use the previous post or add your classes into different java files.

Upvotes: 1

Related Questions