A、B 和 C 类仅位于 src 和MainActivity.class
in 中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)