如何修复非静态错误

时间:2015-05-28 11:44:15

标签: java android-studio static non-static

我的小组正在尝试创建一个联系人列表,其中也可以添加联系人,但是他们收到以下错误:非静态方法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无法执行

1 个答案:

答案 0 :(得分:6)

如果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()