如何处理意图内的非静态成员?
在尝试制作 Android 应用程序时,我在处理静态成员方面遇到了巨大的困难。
以下是应用程序必须执行的操作:
有两个选项卡:“我的课程”和“课程列表”,每个选项卡都有不同的意图。该应用程序的要点是,当您单击“课程列表”中课程旁边的“添加”按钮时,它应该动态更新“我的课程”下的列表。但是,我无法执行此操作,因为保存类的数据结构是动态的,但类列表意图是静态的,因此我不断收到“无法从静态上下文引用非静态成员”错误。
有没有办法使用动态意图,如果可以,如何使用?
编辑:
这是声明意图的位置:
public class TabLayout extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
Intent intent = new Intent(this, MyClasses.class);
tabHost.addTab(tabHost.newTabSpec("My Classes")
.setIndicator("My Classes", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent));
Intent intent2 = new Intent(this, ClassList.class);
tabHost.addTab(tabHost
.newTabSpec("Class List")
.setIndicator("Class List", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent2));
Intent intent3 = new Intent(this, SyncList.class);
tabHost.addTab(tabHost
.newTabSpec("Sync List")
.setIndicator("Sync List", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent3));
tabHost.setCurrentTab(0);
// Set tabs Colors
tabHost.setBackgroundColor(Color.BLACK);
tabHost.getTabWidget().setBackgroundColor(Color.BLACK);
}
}
这是 MyClassList 文件:
public class MyClassList extends MyClasses {
int numClasses;
MyClasses myclasses = new MyClasses();
final static TableRow tableRowArray[] = new TableRow[4];
TextView classNameArray[] = new TextView[4];
TextView teacherNameArray[] = new TextView[4];
Button dropButtonArray[] = new Button[4];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create containers
ScrollView classListScrollView = new ScrollView(this);
TableLayout classListTableLayout = new TableLayout(this);
// Add class entries
for (int i = 0; i < 4; i++)
{
tableRowArray[i] = new TableRow(this);
tableRowArray[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
classNameArray[i] = new TextView(this);
classNameArray[i].setText("");
classNameArray[i].setPadding(10, 20, 10, 20);
classNameArray[i].setTextSize(20);
tableRowArray[i].addView(classNameArray[i]);
teacherNameArray[i] = new TextView(this);
teacherNameArray[i].setText("");
teacherNameArray[i].setPadding(10, 20, 10, 20);
teacherNameArray[i].setTextSize(16);
tableRowArray[i].addView(teacherNameArray[i]);
dropButtonArray[i] = new Button(this);
dropButtonArray[i].setText("Drop");
tableRowArray[i].addView(dropButtonArray[i]);
classListTableLayout.addView(tableRowArray[i], new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tableRowArray[i].setVisibility(View.GONE);
}
classListScrollView.addView(classListTableLayout);
this.setContentView(classListScrollView);
dropButtonArray[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(0);
}
});
dropButtonArray[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(1);
}
});
dropButtonArray[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(2);
}
});
dropButtonArray[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(3);
}
});
}
public void updateMyClasses(ClassObject myclasses[])
{
for (int i = 0; i < 4; i++)
{
if (myclasses[i].isNull)
{
classNameArray[i].setText(myclasses[i].getClassName());
teacherNameArray[i].setText(myclasses[i].getTeacherName());
tableRowArray[i].setVisibility(View.VISIBLE);
}
else
{
tableRowArray[i].setVisibility(View.GONE);
}
}
}
public MyClasses getMyClasses()
{
return myclasses;
}
}
这是 MyClasses 文件:
public class MyClasses extends Activity {
static int numClasses;
static ClassObject MyClassArray[];
public MyClasses()
{
MyClassArray = new ClassObject[4];
for (int i = 0; i < 4; i++)
{
MyClassArray[i] = new ClassObject();
}
numClasses = 0;
}
public int getNumClasses()
{
return numClasses;
}
public void dropClass(int c)
{
for (int i = c; i < 3; i++)
{
MyClassArray[i] = MyClassArray[i+1];
}
//MyClassList.updateMyClasses(MyClassArray);
}
public void addClass(ClassObject c)
{
if (numClasses >= 4)
return;
MyClassArray[numClasses] = c;
//MyClassList.updateMyClasses(MyClassArray);
}
public boolean hasClass(ClassObject c)
{
for (int i = 0; i < 4; i++)
{
if (MyClassArray[i].getClassName() == c.getClassName())
return true;
}
return false;
}
}
这是给出错误的行:
addButtonArray[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
MyClasses.addClass(ClassListArray[0]);
}
});
I'm having a tremendous amount of difficulty dealing with static members while trying to make an Android app.
Here's what the app has to do:
There are two tabs, My Classes and Class List, each of which are separate intents. The point of the app is that when you click the "add" button next to a class in the Class List, it should dynamically update the list under My Classes. However, I can't do this because my data structure that holds the classes is dynamic, but the Class List intent is static, so I keep getting the "Cannot reference non-static member from static context" error.
Is there any way to use dynamic intents, and if so, how?
EDIT:
Here's where the intents are declared:
public class TabLayout extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
Intent intent = new Intent(this, MyClasses.class);
tabHost.addTab(tabHost.newTabSpec("My Classes")
.setIndicator("My Classes", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent));
Intent intent2 = new Intent(this, ClassList.class);
tabHost.addTab(tabHost
.newTabSpec("Class List")
.setIndicator("Class List", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent2));
Intent intent3 = new Intent(this, SyncList.class);
tabHost.addTab(tabHost
.newTabSpec("Sync List")
.setIndicator("Sync List", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent3));
tabHost.setCurrentTab(0);
// Set tabs Colors
tabHost.setBackgroundColor(Color.BLACK);
tabHost.getTabWidget().setBackgroundColor(Color.BLACK);
}
}
Here's the MyClassList file:
public class MyClassList extends MyClasses {
int numClasses;
MyClasses myclasses = new MyClasses();
final static TableRow tableRowArray[] = new TableRow[4];
TextView classNameArray[] = new TextView[4];
TextView teacherNameArray[] = new TextView[4];
Button dropButtonArray[] = new Button[4];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create containers
ScrollView classListScrollView = new ScrollView(this);
TableLayout classListTableLayout = new TableLayout(this);
// Add class entries
for (int i = 0; i < 4; i++)
{
tableRowArray[i] = new TableRow(this);
tableRowArray[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
classNameArray[i] = new TextView(this);
classNameArray[i].setText("");
classNameArray[i].setPadding(10, 20, 10, 20);
classNameArray[i].setTextSize(20);
tableRowArray[i].addView(classNameArray[i]);
teacherNameArray[i] = new TextView(this);
teacherNameArray[i].setText("");
teacherNameArray[i].setPadding(10, 20, 10, 20);
teacherNameArray[i].setTextSize(16);
tableRowArray[i].addView(teacherNameArray[i]);
dropButtonArray[i] = new Button(this);
dropButtonArray[i].setText("Drop");
tableRowArray[i].addView(dropButtonArray[i]);
classListTableLayout.addView(tableRowArray[i], new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tableRowArray[i].setVisibility(View.GONE);
}
classListScrollView.addView(classListTableLayout);
this.setContentView(classListScrollView);
dropButtonArray[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(0);
}
});
dropButtonArray[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(1);
}
});
dropButtonArray[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(2);
}
});
dropButtonArray[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myclasses.dropClass(3);
}
});
}
public void updateMyClasses(ClassObject myclasses[])
{
for (int i = 0; i < 4; i++)
{
if (myclasses[i].isNull)
{
classNameArray[i].setText(myclasses[i].getClassName());
teacherNameArray[i].setText(myclasses[i].getTeacherName());
tableRowArray[i].setVisibility(View.VISIBLE);
}
else
{
tableRowArray[i].setVisibility(View.GONE);
}
}
}
public MyClasses getMyClasses()
{
return myclasses;
}
}
Here's the MyClasses file:
public class MyClasses extends Activity {
static int numClasses;
static ClassObject MyClassArray[];
public MyClasses()
{
MyClassArray = new ClassObject[4];
for (int i = 0; i < 4; i++)
{
MyClassArray[i] = new ClassObject();
}
numClasses = 0;
}
public int getNumClasses()
{
return numClasses;
}
public void dropClass(int c)
{
for (int i = c; i < 3; i++)
{
MyClassArray[i] = MyClassArray[i+1];
}
//MyClassList.updateMyClasses(MyClassArray);
}
public void addClass(ClassObject c)
{
if (numClasses >= 4)
return;
MyClassArray[numClasses] = c;
//MyClassList.updateMyClasses(MyClassArray);
}
public boolean hasClass(ClassObject c)
{
for (int i = 0; i < 4; i++)
{
if (MyClassArray[i].getClassName() == c.getClassName())
return true;
}
return false;
}
}
And here's the line that gives the error:
addButtonArray[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
MyClasses.addClass(ClassListArray[0]);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一行是您的问题:
您试图调用实例方法
addClass
而不提供对MyClasses
实例的引用。如何解决这个问题取决于代码的位置。它是否嵌入在MyClasses
类中?This line is your problem:
You are trying to call the instance method
addClass
without giving the reference to aMyClasses
instance. How you fix this depends on where the code is. Is it embedded in theMyClasses
class?