1

A、B 和 C 类仅位于 src 和MainActivity.classin 中source/com/example/TestApplication

MainActivity 似乎没有看到其他类,因为它们不在同一个位置/包中。如何调用其他类的方法在 MainActivity 中工作。

这是我的代码示例:我想在 MainActivity 中运行 UseActivity 方法

        public class UseActivity extends Activity
        {
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);    
        }
            public String sEntries(View view) 
            {
                DatabaseHandler db = new DatabaseHandler(this);
                String sfinal = "";

                // Inserting Contacts
                Log.d("Insert: ", "Inserting ..");
                db.addContact(new Contact("Ravi", "9100000000"));       
                db.addContact(new Contact("Srinivas", "9199999999"));
                db.addContact(new Contact("Tommy", "9522222222"));
                db.addContact(new Contact("Karthik", "9533333333"));
                switch (view.getId()) 
                {
                case R.id.button1:
                    EditText dbText =  (EditText) findViewById(R.id.editText1);

                     // Reading all contacts
                    Log.d("Reading: ", "Reading all contacts..");
                    List<Contact> contacts = db.getAllContacts();  
                    for (Contact cn : contacts)
                    {
                        sfinal = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
                            // Writing Contacts to sfinal
                        Log.d("Name: ", sfinal);
                        dbText.setText(sfinal);
                    }
                    break;
                }
                return sfinal;
            }      
    }



public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    UseActivity u = new UseActivity();
    {
        u.sEntries(View view);
    }  /////////////////////////// this doesnt work, program underlines UseActivity in red giving error and doesnt see it as another class (maybe because of package)
4

3 回答 3

2

首先,在你的 MainActiviy 中导入 UseActivity 类

import your.package.UseActivity;

其次,在 OnCreate 中,实例化您的 UseActivity 类。

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    UseActivity u = new UseActivity();
    etc...
}
于 2013-07-05T07:24:56.727 回答
1

您需要导入类 A、B 和 C。默认情况下,eclipse 中自动导入的快捷方式至少是:

CNTRL+ SHIFT+ O

我按了这些键很多次,字母都磨损了。

于 2013-07-05T07:21:11.443 回答
0

您必须在 AndroidManifest.xml 中添加第二个活动,然后按照毛的解释 || 使用类似下面的东西

public void YourAction() {
startActivity(new Intent("youpackage.SecondActivity"));
}

<activity
android:label="Second Activity"
android:name=".SecondActivity" >
<intent-filter >
<action android:name="yourpackage.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
于 2013-09-09T03:53:54.900 回答