Classes A, B and C is located only in src and the MainActivity.class
in source/com/example/TestApplication
.
MainActivity doesnt seem to see the other classes because they are not located in the same location/package. How can I call the other classes' methods to work in MainActivity.
Here is an example of my code: I want to run UseActivity methods in MainActivity
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)
First, import UseActivity class in yout MainActiviy
import your.package.UseActivity;
Second, in OnCreate, instantiate your UseActivity class.
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UseActivity u = new UseActivity();
etc...
}
You need to import classes A, B, and C. A shortcut in eclipse for auto-import is, by default at least:
CNTRL+ SHIFT+ O
I have pressed these keys so many times the letters are worn out.
You have to add as a second activity in the AndroidManifest.xml and then do as Mao explained || use something like below
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>
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.