我正在創建一個移動應用程序,並在我的MainActivity類的android studio中,我試圖調用一個內部片段類中的方法。在這個片段類我有另一個類稱爲WebViewInterface:如何訪問java(android)中的內部類?
public class tab2_upgrade extends Fragment {
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab2_upgrade, container, false);
WebView webView = (WebView) view.findViewById(R.id.webView);
webView.loadUrl("file:///android_asset/tab2.html");
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebViewInterface(getActivity()), "Android");
return view;
}
public class WebViewInterface {
WebViewInterface(Context myWebViewFragment) {
context = myWebViewFragment;
}
@JavascriptInterface
public void showToast(String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
但是當我試圖訪問這個內部類從我的MainActivity內它來調用一個方法,我得到,如果它不承認它是一個錯誤(但可以找到它):
@Override
public void onProductPurchased(String productId, TransactionDetails details) {
tab2_upgrade tab2 = new tab2_upgrade();
tab2.WebViewInterface tab2WebView = new tab2.WebViewInterface();
}
或
@Override
public void onProductPurchased(String productId, TransactionDetails details) {
tab2_upgrade tab2 = new tab2_upgrade();
tab2.WebViewInterface tab2WebView = tab2.new WebViewInterface();
}
可能有人請告訴我爲什麼我收到此錯誤,以及如何解決這個問題,這樣我可以訪問WebViewInterfaceç在我的片段類?請參閱附件中的圖片以查看紅色錯誤。由於
您沒有無參數構造函數。 – Adrian