0
即時爲我的項目做一個應用程序,但我不知道該怎麼通過我的方法。呼叫AlertDialog方法與片段內的參數
繼承人我如何調用該方法:
public void scanBar(View v) {
switch (v.getId()) {
case R.id.barcode_scan_btn:
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
showDialog(getActivity(), "No Scanner Found", "Download the scanner application?", "Yes", "No").show();
}
break;
}
}
這裏是我的方法:
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
以下是錯誤:
09-20 05:20:31.774 17032-17032/app.psiteportal.com.psiteportal E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: app.psiteportal.com.psiteportal, PID: 17032
java.lang.IllegalStateException: Could not find a method scanBar(View) in the activity class app.psiteportal.com.psiteportal.DrawerMainAdapter for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'barcode_scan_btn'
at android.view.View$1.onClick(View.java:3994)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: scanBar [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:3987)
我不知道如果我應該將我的方法放在活動或片段中。請幫助
編輯:我需要將參數更改爲片段或只是活動?
你聲明'scanBar()'方法,一個onclick回調方法(使用'安卓onClick'佈局內)? – cylon
嗨@cylon,感謝您的回覆,看來我可以在片段內使用setOnClicklistener因爲我已經讀過這是一個真正的問題,我希望很快會有一個修復,或者我只是不知道如何,愚蠢的我。祝你今天愉快! – d0tzz
我剛剛在一個答案中解釋了這一點。隨意接受:) – cylon