如何修复非静态错误
How to fix non-static error
我的小组正在尝试制作一个也可以添加联系人的联系人列表,但是他们收到以下错误:无法通过静态上下文引用非静态方法 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());
}
}
}
我们的按钮 "add contact" 出现了新的崩溃。其中表示无法执行 populateList
如果 populateList 声明不是 static
.
Check JLS (§8.5).
,则不能引用 MainActivity.populateList();
您必须创建 MainActivity
的实例
MainActivity ma = new MainActivity(); // or another constructor
ma.populateList(); // valid call of method
或者,如果您不需要 MainActivity
的实例,请按如下方式声明 populateList()
:
public static void populateList()
我的小组正在尝试制作一个也可以添加联系人的联系人列表,但是他们收到以下错误:无法通过静态上下文引用非静态方法 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());
}
}
}
我们的按钮 "add contact" 出现了新的崩溃。其中表示无法执行 populateList
如果 populateList 声明不是 static
.
Check JLS (§8.5).
MainActivity.populateList();
您必须创建 MainActivity
MainActivity ma = new MainActivity(); // or another constructor
ma.populateList(); // valid call of method
或者,如果您不需要 MainActivity
的实例,请按如下方式声明 populateList()
:
public static void populateList()