简体   繁体   English

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

[英]How do I deal with non-static members inside of an intent?

I'm having a tremendous amount of difficulty dealing with static members while trying to make an Android app. 在尝试制作Android应用程序时,与静态成员打交道时遇到很多困难。

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: 这是MyClassList文件:

public class MyClassList extends MyClasses { 公共类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;
}

} }

Here's the MyClasses file: 这是MyClasses文件:

public class MyClasses extends Activity { 公共类MyClasses扩展了活动{

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. 您正在尝试调用实例方法addClass而不提供对MyClasses实例的引用。 How you fix this depends on where the code is. 您如何解决此问题取决于代码在哪里。 Is it embedded in the MyClasses class? 它嵌入在MyClasses类中吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对非静态方法进行静态引用? - How do i make a static reference to a non-static method? 如何在非静态方法中从另一个类调用非静态方法? (java) - How do I call a non-static method from another class in a non-static method? (java) 非静态方法如何访问java中的静态成员? - How does non-static method access static members in java? 我需要在静态类中使用非静态变量……如何解决这个问题? - I need to use a non-static variable inside a static class… How to get around this? 我如何一劳永逸地解决这种通过方法访问的非静态和静态变量? - How do i solve this non-static and static variables accessing through methods for once and for all? 具有静态成员的非静态访问的Java 8类型推断 - Java 8 type inference with non-static access of static members Java 8 - 如何声明对返回void的未绑定非静态方法的方法引用 - Java 8 - how do I declare a method reference to an unbound non-static method that returns void 如何从Groovy构造(非静态)Java内部类 - How do I construct a (non-static) Java inner class from Groovy java - 如何在java中不相关的类之间传递非静态变量? - How do I pass non-static variables between unrelated classes in java? 通过Java中的main方法访问非静态成员 - Accessing non-static members through the main method in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM