简体   繁体   中英

Call a View from a class which extends Activity

I've got a java class that extends Activity (not AppCompatActivity!).

Now it happens that I want to call a View which was defined in an .xml file.
How do I do that?
My app keeps crashing, but if I let the .java class extend AppCompatActivity, it doesn't.

Any ideas on how ti fix that?

Sadly it has to extend Activity.

The .java class

public class pop extends Activity {

    @Override
    protected void onResume() {
        super.onResume();

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int) (width * .8), (int) (height * .4)); // Width * 0,8 == 80% der Fenstergröße

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Thats where it fails, because it couldnt get the parent-view.
                parent.setBackgroundColor(Color.argb(250,0,0,0));
                finish();
            }
        });
    }

}

pop.java

public class pop extends Activity {

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

    }

    @Override
    protected void onResume() {
        super.onResume();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Thats where it fails, because it couldnt get the parent-view.
                parent.setBackgroundColor(Color.argb(250,0,0,0));
                finish();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

}

I have already found teh solution, thank you all foor the help! I really appreciate that!

Here's one way (as mentioned in the comments) utilising onResume , it's all handled in the invoking activity basically by setting a variable to indicate that the pop activity has been started ( note as you may be using a dialog that clicking out side the dialog would have the same effect ).

This is pretty limited.

public class MainActivity extends AppCompatActivity {

    boolean resumestate = false;
    LinearLayout ll; //The main layout

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

        ll = (LinearLayout) findViewById(R.id.mainactivity_ll);

    }

    // Invoke the pop activity, setting resumestate to indicate this      
    public void doTestButton(View v) {
        Toast.makeText(this,"You Clicked the Test Button",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,Pop.class);
        startActivity(intent);
        resumestate = true;
    }

    //Check if the activity was invoked and if so set background colour
    @Override
    protected void onResume() {
        super.onResume();
        if (resumestate) {
            ll.setBackgroundColor(Color.argb(250,0,0,0));
            resumestate = false;
        }
    }
}

Another way, using startActivityForresult and thus returning the background colour via a returned Intent (more flexible) :-

MainActivty

public class MainActivity extends AppCompatActivity {

    boolean resumestate = false;
    LinearLayout ll;
    int requestcode = 10;

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

        ll = (LinearLayout) findViewById(R.id.mainactivity_ll);

    }

    public void doTestButton(View v) {
        Toast.makeText(this,"You Clicked the Test Button",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,Pop.class);
        startActivityForResult(intent,requestcode);
        resumestate = true;
    }

    @Override
    protected void onActivityResult(int rqstcode, int resultcode, Intent data) {
        if (rqstcode == requestcode) {
            if (resultcode == Activity.RESULT_OK) {
                int newcolour = data.getIntExtra("BGRNDCOLOUR",Color.argb(250,48,48,48));
                ll.setBackgroundColor(newcolour);
            }
        }
    }
}

The button's onClick method in the pop activity :-

public void doTestButton(View v) {
    int newcolour = Color.argb(250,128,128,128);
    Intent ri = new Intent();
    ri.putExtra("BGRNDCOLOUR", newcolour);
    setResult(Activity.RESULT_OK,ri);
    finish();
}

A third way, which I think may be considered bad practice, would be to create a public static method to alter the background in conjunction with declaring the static variable for respective layout.

eg In the mainactivty replace LinearLayout ll; with static LinearLayout ll; and then add the appropriate method to main activity eg

    public static void alterBackGroundColour(int newcolour) {
        ll.setBackgroundColor(newcolour);
    }

then in the appropriate place in the pop activity use :-

    MainActivity.alterBackGroundColour(Color.argb(250,128,0,128));

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