表单程序生成的布局为 XML 中的布局 android
Form program generated layout to XML layout in android
最近学习了android例子的代码,是录音的例子,有两个自定义的classes扩展按钮eg.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,但是return找不到class的错误
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设计器视图中,它还有一个
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>
有没有我遗漏的东西,我检查了包名是正确的。我该如何解决?
更新:清单
<?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>
首先,尝试 classical clean/rebuild 项目。它将确保您的代码确实存在问题,而不是调试器
然后,我建议你在不同的文件中声明你的两个 classes,它会更具可读性,而且使用内部 classes 时更容易出现问题。
我看到您的问题与此 post 中的问题有关: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?
您正在为按钮使用内部 class。那么你必须使用它的方式是不同的。使用之前的 post 或将您的 class 添加到不同的 java 文件中。
最近学习了android例子的代码,是录音的例子,有两个自定义的classes扩展按钮eg.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,但是return找不到class的错误
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设计器视图中,它还有一个
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>
有没有我遗漏的东西,我检查了包名是正确的。我该如何解决?
更新:清单
<?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>
首先,尝试 classical clean/rebuild 项目。它将确保您的代码确实存在问题,而不是调试器
然后,我建议你在不同的文件中声明你的两个 classes,它会更具可读性,而且使用内部 classes 时更容易出现问题。
我看到您的问题与此 post 中的问题有关: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?
您正在为按钮使用内部 class。那么你必须使用它的方式是不同的。使用之前的 post 或将您的 class 添加到不同的 java 文件中。