如何解决捕获或最终预期的错误?

时间:2018-12-16 06:26:38

标签: java android

如何解决Android Studio中的“'捕获'或'最终'预期”错误??

a screenshot of the error

public class FragmentRecent extends Fragment {

    View root_view, parent_view;
    private RecyclerView recyclerView;
    private AdapterChannel adapterChannel;
    private SwipeRefreshLayout swipeRefreshLayout;
    private Call<CallbackChannel> callbackCall = null;
    private int post_total = 0;
    private int failed_page = 0;
    private InterstitialAd interstitialAd;
    private OfflineDatabase databaseHelper;
    int counter = 3;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root_view = inflater.inflate(R.layout.fragment_recent, null);
        parent_view = getActivity().findViewById(R.id.main_content);

        loadInterstitialAd();

        swipeRefreshLayout = (SwipeRefreshLayout) root_view.findViewById(R.id.swipe_refresh_layout_home);
        swipeRefreshLayout.setColorSchemeResources(R.color.orange, R.color.green, R.color.blue, R.color.red);

        recyclerView = (RecyclerView) root_view.findViewById(R.id.recyclerViewHome);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);


        //set data and list adapter
        adapterChannel = new AdapterChannel(getActivity(), recyclerView, new ArrayList<Channel>());
        recyclerView.setAdapter(adapterChannel);

        // on item list clicked
        adapterChannel.setOnItemClickListener(new AdapterChannel.OnItemClickListener() {
            @Override
            public void onItemClick(View v, Channel obj, int position) {
                Intent intent = new Intent(getActivity(), ActivityDetailChannel.class);
                intent.putExtra(Constant.KEY_CHANNEL_CATEGORY, obj.category_name);
                intent.putExtra(Constant.KEY_CHANNEL_ID, obj.channel_id);
                intent.putExtra(Constant.KEY_CHANNEL_NAME, obj.channel_name);
                intent.putExtra(Constant.KEY_CHANNEL_IMAGE, obj.channel_image);
                intent.putExtra(Constant.KEY_CHANNEL_URL, obj.channel_url);
                intent.putExtra(Constant.KEY_CHANNEL_DESCRIPTION, obj.channel_description);
                startActivity(intent);

                showInterstitialAd();
            }
        });

        // detect when scroll reach bottom
        adapterChannel.setOnLoadMoreListener(new AdapterChannel.OnLoadMoreListener() {
            @Override
            public void onLoadMore(int current_page) {
                if (post_total > adapterChannel.getItemCount() && current_page != 0) {
                    int next_page = current_page + 1;
                    requestAction(next_page);
                } else {
                    adapterChannel.setLoaded();
                }
            }
        });

        // on swipe list
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (callbackCall != null && callbackCall.isExecuted()) callbackCall.cancel();
                adapterChannel.resetListData();
                requestAction(1);
            }
        });

        requestAction(1);


        return root_view;
    }

    private void displayApiResult(final List<Channel> channels) {
        adapterChannel.insertData(channels);
        swipeProgress(false);
        if (channels.size() == 0) {
            showNoItemView(true);
        }
    }

    private void requestListPostApi(final int page_no) {
        ApiInterface apiInterface = RestAdapter.createAPI();
        callbackCall = apiInterface.getPostByPage(page_no, Config.LOAD_MORE);
        callbackCall.enqueue(new Callback<CallbackChannel>() {
            @Override
            public void onResponse(Call<CallbackChannel> call, Response<CallbackChannel> response) {
                CallbackChannel resp = response.body();
                if (resp != null && resp.status.equals("ok")) {
                    post_total = resp.count_total;
                    displayApiResult(resp.posts);
                } else {
                    onFailRequest(page_no);
                }
            }

            @Override
            public void onFailure(Call<CallbackChannel> call, Throwable t) {
                if (!call.isCanceled()) onFailRequest(page_no);
            }

        });
    }

    private void onFailRequest(int page_no) {
        failed_page = page_no;
        adapterChannel.setLoaded();
        swipeProgress(false);
        if (NetworkCheck.isConnect(getActivity())) {
        } else {
            //showToast("Internet Not");
            if (databaseHelper.getOfflineData("FragmentCategory").length() != 0) {
                setJson(databaseHelper.getOfflineData("FragmentCategory"), false);
            }
        }
    }
    //databaseHelper.removeAll();


    private void requestAction(final int page_no) {
        showFailedView(false, "");
        showNoItemView(false);
        if (page_no == 1) {
            swipeProgress(true);
        } else {
            adapterChannel.setLoading();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                requestListPostApi(page_no);
            }
        }, Constant.DELAY_TIME);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        swipeProgress(false);
        if (callbackCall != null && callbackCall.isExecuted()) {
            callbackCall.cancel();
        }
    }

    private void showFailedView(boolean show, String message) {
        View lyt_failed = (View) root_view.findViewById(R.id.lyt_failed_home);
        ((TextView) root_view.findViewById(R.id.failed_message)).setText(message);
        if (show) {
            recyclerView.setVisibility(View.GONE);
            lyt_failed.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            lyt_failed.setVisibility(View.GONE);
        }
        ((Button) root_view.findViewById(R.id.failed_retry)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestAction(failed_page);
            }
        });
    }

    private void showNoItemView(boolean show) {
        View lyt_no_item = (View) root_view.findViewById(R.id.lyt_no_item_home);
        ((TextView) root_view.findViewById(R.id.no_item_message)).setText(R.string.no_post_found);
        if (show) {
            recyclerView.setVisibility(View.GONE);
            lyt_no_item.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            lyt_no_item.setVisibility(View.GONE);
        }
    }


    private void swipeProgress(final boolean show) {
        if (!show) {
            swipeRefreshLayout.setRefreshing(show);
            return;
        }
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(show);
            }
        });
    }

    public void setJson(String result, Boolean isOnline) {
        try {

            //inseting result to database
            if(isOnline) {
                ContentValues offline_data = new ContentValues();

                offline_data.put(OfflineDatabase.KEY_OFFLINE_DATA, result);

                if(databaseHelper.getOfflineData("FragmentCategory").length()!=0) {
                    databaseHelper.update("FragmentCategory",offline_data);
                } else {
                    offline_data.put(OfflineDatabase.KEY_ACTIVITY_NAME, "FragmentCategory");
                    databaseHelper.addOfflineData(offline_data, null);
                    //handle both exceptions
                }
            }}}
    private void loadInterstitialAd() {
        if (Config.ENABLE_ADMOB_INTERSTITIAL_ADS) {
            interstitialAd = new InterstitialAd(getActivity());
            interstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_unit_id));
            interstitialAd.loadAd(new AdRequest.Builder().build());
            interstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    interstitialAd.loadAd(new AdRequest.Builder().build());
                }
            });
        } else {
            Log.d("AdMob", "AdMob Interstitial is Enabled");
        }
    }

    private void showInterstitialAd() {
            if (Config.ENABLE_ADMOB_INTERSTITIAL_ADS) {

                if (interstitialAd != null && interstitialAd.isLoaded()) {

                    if (counter == Config.ADMOB_INTERSTITIAL_ADS_INTERVAL) {
                        interstitialAd.show();
                        counter = 1;
                    } else {
                        counter++;
                    }

                } else {
                    Log.d("AdMob", "Interstitial Ad is Disabled");
                }
            } else {
                Log.d("AdMob", "AdMob Interstitial is Disabled");
            }
        }}

1 个答案:

答案 0 :(得分:0)

public static void waitVisibilityOfElementLocated(WebDriver driver, String locator) { String key = ""; WebElement element = null; try { key = Utility.fetchLocatorKey(locator); } catch (Exception e) { System.out.println("Exception in getText method, " + e.getMessage()); } if (key.endsWith("id")) { WebDriverWait wait = new WebDriverWait(driver, 60); element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(locator))); } else if (key.endsWith("cssselector")) { WebDriverWait wait = new WebDriverWait(driver, 60); element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(locator))); } else if (key.endsWith("linktext")) { WebDriverWait wait = new WebDriverWait(driver, 60); element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(locator))); } else if (key.endsWith("xpath")) { WebDriverWait wait = new WebDriverWait(driver, 60); element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(locator))); } } 开始:

try

必须继续:

public void setJson(String result, Boolean isOnline) {
    try {
       ...
    }
    ...
}

try { ... } catch(Exception e) { } finally { } catch是可选的(取决于finally的处理位置)。