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]);
}
});
This line is your problem:
MyClasses.addClass(ClassListArray[0]);
You are trying to call the instance method addClass
without giving the reference to a MyClasses
instance. How you fix this depends on where the code is. Is it embedded in the MyClasses
class?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.