我在Android编程方面相当新,所以我希望这个问题对你们来说很容易。
我打开一个带有额外按钮和AlertDialog
的自定义seekbar
,以显示播放歌曲的进度。我的问题是我无法访问该按钮的onClick
方法。
我使用以下代码打开AlertDialog:
public void openAudioPlayer(View view){
new AlertDialog.Builder(this)
.setTitle(R.string.audio_player_title)
.setMessage(R.string.audio_player_description)
.setView(R.layout.dialog_audio_player)
.setPositiveButton(R.string.confirm,null)
.setNegativeButton(R.string.cancel, null).show();
}
其中dialog_audio_player
是自定义XLM文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/playerBox"
android:layout_centerVertical="true">
<ToggleButton
android:id="@+id/playStateButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:background="@drawable/toggle_player"
android:onClick="onPlayerClicked"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff=""
android:textOn="" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/playStateButton" />
</RelativeLayout>
</LinearLayout>
所以,我希望使用自定义onPlayerCLicked
方法控制按下该按钮,该方法包含在openAudioPlayer
:
public void onPlayerClicked(View view){
Toast.makeText(this,"clicked",Toast.LENGTH_LONG).show();
}
但是程序从不进入那里。事实上,该应用程序关闭。
我该怎么做?
崩溃如下:
06-04 12:47:13.587 3928-3928/vmc.songbook E/AndroidRuntime: FATAL EXCEPTION: main
Process: vmc.songbook, PID: 3928
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4698)
at android.view.View.performClick(View.java:5610)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4693)
at android.view.View.performClick(View.java:5610)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
at vmc.songbook.RecordAudio.stopRecording(RecordAudio.java:121)
at vmc.songbook.RecordAudio.onToggleClicked(RecordAudio.java:84)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4693)
at android.view.View.performClick(View.java:5610)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:1)
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.dialog_audio_player , null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
ToggleButton btn = (ToggleButton) view.findViewById(R.id.playStateButton);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btn has been clicked
}
});
alertD.setView(view);
alertD.show();
答案 1 :(得分:1)
更新您的方法def divide(alist):
def _inner(left=0, right=len(alist)): # default args based on comment @EricDuminil
middle = (right - left) // 2
# when the list have only one element, it should return the 0
if middle == 0:
alist[left:right] = [0]
else:
_inner(left, left+middle)
_inner(left+middle, right)
return _inner()
temp = [1, 2, 3, 4, 5, 6]
divide(temp)
print(temp)
# [0, 0, 0, 0, 0, 0]
,如下所示:
openAudioPlayer()