简体   繁体   English

如何修复 Android Studio 中“从未使用过分配给“变量”的值 true”?

[英]how to fix "The value true assigned to "variable" is never used" in Android Studio?

I am making this eCommerce app, And, i wanted this behaviour in it.我正在制作这个电子商务应用程序,并且我希望其中包含这种行为。 Which is- At the opening, there will be pitures in the MainActivity.也就是——在开始的时候,MainActivity 中会有 pitures。 And a search box.还有一个搜索框。

When you click the picture it will take you to MainActivity2 and there is an ImageView in this activity which will change to become the same picture as the picture I clicked in MainActivity.当你点击图片时,它会带你到MainActivity2,这个活动中有一个ImageView,它会变成与我在MainActivity中点击的图片相同的图片。

And

When you search something in the search box in the MainActivity, if your searched text matches with the string object of the arraylist in the MainActivity2, it will change the ImageView of MainActivity2 with the corresponding image of the string object of the arraylist.当您在MainActivity中的搜索框中搜索内容时,如果您搜索到的文本与MainActivity2中的arraylist的字符串object匹配,则会将MainActivity2中的ImageView替换为arraylist中的字符串object对应的图像。

I hope, i made minimum grammatical mistakes to make you understand my problem.我希望,我犯的语法错误最少,可以让您理解我的问题。

Anyways, For the first case- I tried to make this work by using On touchListeners, Int.nets and booleans.不管怎样,对于第一种情况——我试图通过使用 On touchListeners、Int.nets 和布尔值来完成这项工作。

As you can see in the code below, touching my ImageView(iBtn1) will trigger Intent intent1, make boolean i1 =true and then send the boolean to next activity in the name "t1".正如您在下面的代码中看到的,触摸我的 ImageView(iBtn1) 将触发 Intent intent1,使 boolean i1 = true,然后将 boolean 发送到名为“t1”的下一个活动。

And, For the second case- I tried using OnClickListeners, Int.nets, arraylists and booleans.而且,对于第二种情况——我尝试使用 OnClickListeners、Int.nets、数组列表和布尔值。

First, I changed the Edittext(input) into string.首先,我将 Edittext(input) 更改为字符串。 For clicking the search Button(igo), it will trigger Intent intent, Then it will send the string Data of the input as "eText" and send boolean data x=true, as "x".对于点击搜索按钮(igo),它会触发Intent意图,然后它将输入的字符串数据作为“eText”发送,并发送boolean数据x = true,作为“x”。

public class MainActivity extends AppCompatActivity {

EditText input;
ImageView iBtn1;
TextView tName, tPrice, tDescription;
ImageView iImage;
Button igo;
String eText;

boolean i1,x;


@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


            i1=false;
          
            x=false;

    igo=findViewById(R.id.go);

    iBtn1=findViewById(R.id.Btn1);
    
    input=findViewById(R.id.edittext);

    eText=input.getText().toString();

    igo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            x=true;

            Intent intent=new Intent(MainActivity.this,MainActivity2.class);

            intent.putExtra("eText",eText);
            intent.putExtra("x",x);
            startActivity(intent);
        }
    });





    iBtn1.setOnTouchListener((v, event) -> {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

          v.setPressed(true);
          iBtn1.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.ectangle_31_1));
            return true;

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            iBtn1.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.ectangle_31));
            overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
            i1=true;
            Intent intent1=new Intent(MainActivity.this,MainActivity2.class);



            intent1.putExtra("t1",i1);

            startActivity(intent1);
            v.performClick();
            v.setPressed(false);
            return false;
        } else {
            return false;
        }


    });

In the next activity-在接下来的活动中——

For my first case- My target ImageView is iImage.对于我的第一个案例——我的目标 ImageView 是 iImage。 But at first, what i did was, get the (boolean) data from the previous Intent intent1 to the current Intent I1.但起初,我所做的是,将(布尔)数据从先前的 Intent intent1 获取到当前的 Intent I1。 Then i have declared a variable(x1) containing the value of that boolean data.然后我声明了一个变量(x1),其中包含该 boolean 数据的值。

Then i have implemented an if condition, If x1 is true, it will change the image resource of the ImageView iImage.然后我实现了一个 if 条件,如果 x1 为真,它将更改 ImageView iImage 的图像资源。 Else, nothing happens.否则,什么也不会发生。

And, for my 2nd case-而且,对于我的第二个案例-

I have this arraylist made up.我编了这个 arraylist。 It contained string data as xName and imageId as xImageId.它包含作为 xName 的字符串数据和作为 xImageId 的 imageId。 Anyways, First i got the string data from "eText" and boolean data using getIntent.无论如何,首先我使用 getIntent 从“eText”和 boolean 数据中获取了字符串数据。

Then implementating if condition, i tried to compare the string data with the string of arraylist. If it is equal.然后执行 if 条件,我尝试将字符串数据与 arraylist 的字符串进行比较。如果相等。 Then i told the programm to change the ImageView just like the case-1然后我告诉程序像案例 1 一样更改 ImageView

And, i got the data而且,我得到了数据

public class MainActivity2 extends AppCompatActivity {

String eName;

boolean bx;


ImageView iImage;
TextView iname;

Intent I1,intent;


ArrayList<x> xList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);



    iImage=findViewById(R.id.xblock);
    iname=findViewById(R.id.xname);


    xList=new ArrayList<>();

    xList.add(new x("engineer",R.drawable.ectangle_32));
    xList.add(new x("Artists", R.drawable.ectangle_31));
    xList.add(new x("Singers",R.drawable.ectangle_33));
    xList.add(new x("Lawyer",R.drawable.ectangle_37));
    xList.add(new x("Writters",R.drawable.ectangle_38));
    xList.add(new x("developer",R.drawable.ectangle_36));




    I1=getIntent();
    intent=getIntent();


    eName = intent.getStringExtra("eText");
    bx = intent.getBooleanExtra("x",false);



    if (bx=true) {

      String yname = eName.toLowerCase();

      for (x yList : xList) {

       String xname = yList.getxName().toLowerCase();

          if (xname.equals(yname)) {
           iname.setText(yList.getxName());
           iImage.setImageResource(yList.getxImageId());

          }


      }
    }

    boolean x1= intent.getBooleanExtra("t1",false);

   if (x1=true){
        iImage.setImageResource(R.drawable.ectangle_31);

    }
   else{x1 = false;}
}

} }

But, my app crashes, It can't get to MainActivity2, And when check the problem in MainActivity2, it say that my variable is not assigned.但是,我的应用程序崩溃了,它无法访问 MainActivity2,并且在检查 MainActivity2 中的问题时,它说我的变量未分配。

What is the problem here?这里有什么问题?

Thanks a lot for reading, all this gibberish writing.非常感谢阅读,所有这些乱码。 Please let me know if i hade made any mistakes.如果我犯了任何错误,请告诉我。

I think the problem is in getting boolean bx It shouldbe like this我认为问题在于获取 boolean bx 应该是这样的

boolean bx = getIntent().getBooleanExtra(x);

try it if it works试试看是否有效

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

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