如何处理意图内的非静态成员?

时间:2011-10-12 22:22:24

标签: java android static

在尝试制作Android应用时遇到静态成员时遇到了很大的困难。

这是应用程序必须执行的操作:

有两个选项卡,My Classes和Class List,每个选项卡都是单独的意图。该应用程序的要点是,当您单击类列表中类的旁边的“添加”按钮时,它应该动态更新“我的类”下的列表。但是,我不能这样做,因为我保存类的数据结构是动态的,但是类列表意图是静态的,所以我不断得到“无法从静态上下文引用非静态成员”错误。

有没有办法使用动态意图,若然,怎么办?

编辑:

这是声明意图的地方:

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扩展了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文件:

公共类MyClasses扩展了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]);
            }
        });

1 个答案:

答案 0 :(得分:0)

这一行是你的问题:

MyClasses.addClass(ClassListArray[0]);

您尝试调用实例方法addClass,而不提供对MyClasses实例的引用。如何解决这个问题取决于代码的位置。它是否嵌入MyClasses类?