从扩展Activity的类调用View

时间:2017-07-23 10:11:26

标签: java android android-activity

我有一个扩展Activity的java类(不是AppCompatActivity!)。

现在我想调用一个在.xml文件中定义的View 我该怎么做?
我的应用程序一直在崩溃,但如果我让.java类扩展AppCompatActivity,它就不会。

关于如何解决问题的任何想法?

可悲的是,它必须扩展活动。

.java类

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) {

            }
        });
    }

}

我已经找到了解决方案,谢谢大家的帮助!我真的很感激!

1 个答案:

答案 0 :(得分:1)

这里的一种方式(如评论中所述)利用onResume,它主要通过设置一个变量来指示pop活动已经开始,在调用活动中处理它们( 注意,因为您可能正在使用一个对话框,点击对话框会产生相同的效果

这是非常有限的。

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;
        }
    }
}

另一种方法,使用startActivityForresult并通过返回的Intent返回背景颜色(更灵活): -

<强> 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);
            }
        }
    }
}

onClick活动中按钮的pop方法: -

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();
}

第三种方法,我认为可能被认为是不好的做法,就是创建一个public static方法来改变背景,同时为相应的布局声明静态变量。

e.g。在mainactivty中将LinearLayout ll;替换为static LinearLayout ll;,然后将适当的方法添加到主要活动,例如

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

然后在pop活动的适当位置使用: -

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