简体   繁体   中英

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. 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.

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.

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.

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".

And, For the second case- I tried using OnClickListeners, Int.nets, arraylists and booleans.

First, I changed the Edittext(input) into string. 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".

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. But at first, what i did was, get the (boolean) data from the previous Intent intent1 to the current Intent I1. Then i have declared a variable(x1) containing the value of that boolean data.

Then i have implemented an if condition, If x1 is true, it will change the image resource of the ImageView iImage. Else, nothing happens.

And, for my 2nd case-

I have this arraylist made up. It contained string data as xName and imageId as xImageId. Anyways, First i got the string data from "eText" and boolean data using getIntent.

Then implementating if condition, i tried to compare the string data with the string of arraylist. If it is equal. Then i told the programm to change the ImageView just like the case-1

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.

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 = getIntent().getBooleanExtra(x);

try it if it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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