最近我研究了android示例中的代码,它是录制音频的示例,并且有两个自定义类扩展按钮,例如RecordButton和PlayButton
代码如下:
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);
}
}
我想将其更改为 XML ,但是,它返回类未找到的错误
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
在 XML 设计器 View 中,它还有一个
java.lang.LinkageError (loader attempt a duplicate class definition for name : RecordButton)
这是我尝试构建的 XML:
<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>
有没有我遗漏的东西,我检查了包名称是否正确。我该如何解决?
更新: list
<?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>
最佳答案
首先,尝试经典的清理/重建项目。它将确保您的代码确实存在问题,而不是调试器存在问题
然后,我建议您在不同的文件中声明两个类,这样会更具可读性,而且在使用内部类时会更频繁地出现问题。
我发现您的问题与这篇文章中的问题有关:extend button android, xml layout ,你尝试过给出的答案吗?
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?
您正在为按钮使用内部类。那么你使用它的方式就不同了。使用上一篇文章或将您的类添加到不同的 java 文件中。
关于java - android中表单程序生成的布局到XML布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34504104/