gpt4 book ai didi

java - 如何修复非静态错误

转载 作者:行者123 更新时间:2023-11-29 06:58:12 28 4
gpt4 key购买 nike

我的小组正在尝试制作一个联系人列表,其中也可以添加联系人,但是他们收到以下错误:无法通过静态上下文引用非静态方法 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 声明不是static,则不能引用 MainActivity.populateList();
Check JLS (§8.5) .

您必须创建 MainActivity 的实例

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

或者,如果您不需要 MainActivity 的实例,请按如下方式声明 populateList():

public static void populateList()

关于java - 如何修复非静态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30505667/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com