我的小组正在尝试创建一个也可以添加联系人的联系人列表,但是他们遇到以下错误:无法通过静态上下文引用非静态方法populatelist()。但是,我们不知道这样是否真的有效。任何帮助表示赞赏

public class Profile extends Activity {

    ImageView contactImageImgView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);

        Button editProfileButton = (Button) findViewById(R.id.BeditProfile);
        Button addContactButton = (Button) findViewById(R.id.AdContactButton);
        //Text ChangeProfilePictureText = (Text) findViewById(R.id.ChangeProfilePicture);

        String Name = getIntent().getStringExtra("Name");
        String eMail = getIntent().getStringExtra("Mail");
        String Mobile = getIntent().getStringExtra("Mobile");
        String Adress = getIntent().getStringExtra("Adress");
        String Bio = getIntent().getStringExtra("Bio");


        contactImageImgView = (ImageView) findViewById(R.id.imgViewContactImage);
        TextView tv_Name = (TextView) findViewById(R.id.Name);
        TextView tv_Mail = (TextView) findViewById(R.id.Email);
        TextView tv_Mobile = (TextView) findViewById(R.id.Mobile);
        TextView tv_Adress = (TextView) findViewById(R.id.Adress);
        TextView tv_Bio = (TextView) findViewById(R.id.Bio);

        tv_Name.setText(Name);
        tv_Mail.setText(eMail);
        tv_Mobile.setText(Mobile);
        tv_Adress.setText(Adress);
        tv_Bio.setText(Bio);

        if(Cube.fromUnit){
            editProfileButton.setVisibility(View.GONE);
            //ChangeProfilePictureText.replaceWholeText("");
            tv_Name.setText(Cube.Name);
            tv_Mail.setText(Cube.eMail);
            tv_Mobile.setText(Cube.Mobile);
            tv_Adress.setText(Cube.Adress);
            tv_Bio.setText(Cube.Bio);
        }

        contactImageImgView.setOnClickListener(new View.OnClickListener() {

            public void onClick (View v){  // error @ View v, cannot resolve symbol v , expected ;
                Intent intent = new Intent();
                intent.setType("image*/");
                intent.setAction(intent.ACTION_GET_CONTENT);
                startActivityForResult(intent.createChooser(intent, "Select Profile Image"), 1);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_maps, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.menu_home) {
            Intent i = new Intent(this, HomeScreen.class);
            startActivity(i);
        }
        return super.onOptionsItemSelected(item);
    }

    public void onButtonClick(View v) {
        if (v.getId() == R.id.BeditProfile) {
            Intent i = new Intent(Profile.this, editProfile.class);
            startActivity(i);
        }

        if (v.getId() == R.id.AdContactButton) {
            MainActivity.addContact(Cube.Name, Cube.Adress, "", Cube.Mobile, Cube.eMail);
            MainActivity.populateList();
            Toast pass = Toast.makeText(this, Cube.Name + " " + "Has been added to your contacts", Toast.LENGTH_LONG);
            pass.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 10);
            pass.show();
        }
    }

    public void onActivityResult(int reqCode, int resCode, Intent data) {
        if(resCode == RESULT_OK){
            if(reqCode == 1)
                contactImageImgView.setImageURI(data.getData());
        }
    }
}


关于按钮“添加联系人”,我们发生了新的崩溃。上面说无法执行populateList

最佳答案

如果populateList声明不是MainActivity.populateList();Check JLS (§8.5),则不能引用static

您必须创建MainActivity的实例

MainActivity ma = new MainActivity(); // or another constructor
ma.populateList();   // valid call of method


或者,如果不需要MainActivity的实例,请声明populateList(),如下所示:

public static void populateList()

07-25 22:24