简体   繁体   English

为什么每次我实现这个逻辑时我的应用程序都会崩溃?

[英]Why is My App Crashing everytime I implement this Logic?

There are multiple cardView in the App, and Click on each Card a new Intent is launched. App中有多个cardView,点击每张卡片就会启动一个新的Intent。 Also before launching the Intent I am passing a puExtra along.同样在启动 Intent 之前,我正在传递一个 puExtra。

  final String label = holder.text_label.getText().toString().trim();

    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position == 0 && label.equalsIgnoreCase("Alt. English")) {

                Intent intent = new Intent(context, ShowPDFActivity.class);
                intent.putExtra("AltE","AltE");
                context.startActivity(intent);

            }
        }
    });

And in my another Activity, I have created an Array of Titles, where i Have stored all the possible values which are received in this Activity.在我的另一个活动中,我创建了一个标题数组,我在其中存储了在此活动中收到的所有可能值。

 String[] title = {
            "AltE",
            "Alt.English2013"
    };

Now I am running a for Loop to see what value is received and accordingly perform the functions.现在我正在运行 for 循环以查看接收到的值并相应地执行功能。

Intent intent = getIntent();
    for (int i = 0; i <= title.length; i++) {
        Log.e("Length ", String.valueOf(title.length));

        a1 = intent.getStringExtra(title[i]);

        if (a1.equalsIgnoreCase("AltE")) {

            String pdf = "https://www.researchgate.net/STUDIES.pdf";
            webView.loadUrl("http://docs.google.com/viewer?url=" + pdf);

            webView.setWebViewClient(new WebViewClient() {

                public void onPageFinished(WebView view, String url) {
                    // do your stuff here
                    progressbar.setVisibility(View.GONE);
                }
            });


        } else {
            Toast.makeText(this, "In Else", Toast.LENGTH_SHORT).show();
        }

But the App crashes with this Message :但是应用程序崩溃了这个消息:

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference

What Am i Doing Wrong and how can this be Fixed ?我做错了什么,如何解决?

First reverse: from第一个反转:从

if (a1.equalsIgnoreCase("AltE")) {

to

   if ("AltE".equalsIgnoreCase(a1)) {

because "AltE" will not be equal to null will make it stop crashing.因为“AltE”将不等于 null 将使其停止崩溃。

Also the reason for your crash is that getStringExtra returns default null and you are checking for each title, rather than just one.崩溃的另一个原因是 getStringExtra 返回默认 null 并且您正在检查每个标题,而不仅仅是一个。 and so for the "Alt.English2013" you don't have a value, according to your story.因此,根据您的故事,对于"Alt.English2013"您没有价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM