How to move from one class to other class in android - android

I want to know how to move from one class to other class in android.
I have one main class i.e Addition.java(Main Activity) and i created other subactivity(Form.java).
I want to how to make my move from one class(ie.Activity)to other.
I tried this,but not working out,just trying to figure out
Intent intent = new Intent();
intent.setClass(this.getParent(),Form.class);
startActivity(intent);
here Form.class is the subactivity, this.getParent I hope it represents main activity. And I created one activity in manifest.xml file and named it as .Form
Am i working right?

The below code works perfectly.
Try it:
#Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(v.getContext(),Form.class);
startActivity(intent);
}

make sure that activity is declared in Android.manifest.

Related

Navigate Between Activities (Android Studio)

I got a problem when I navigate between two activities, it shows me error and I don't know what is the problem. I am very sure that my code is correct, because it just simple Intent navigate by on click Button.
When I Press the button to go to the next activity it returns me to the fist activity (not the desire one). Note that both activity has background image.
Fist Activity
public class firstActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);//has a background img and one button
}
public void nextPage(View view){
Intent StartNewActivity = new Intent(firstActivity.this, secondActivity.class);
startActivity(StartNewActivity);
overridePendingTransition(R.layout.slide_in_up, R.layout.slide_out_up);
}
}
Second Activity
public class secondActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);// has a background img and one button
}
public void nextPage(View view){
Intent StartNewActivity = new Intent(secondActivity.this, thirdActivity.class);
startActivity(StartNewActivity);
overridePendingTransition(R.layout.slide_in_up, R.layout.slide_out_up);
}
}
This is the error message
Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
Also, I did not use any ripple drawable in my app.Even though I don't know what does it mean?
Thanks,
Something you have wants to to find a refwrence to that ripple component, you need to find out what.
Otherwise, you can try to make sure you have added a reference to the support.v7.widget in the second activity and see if the exception goes away.
Aside from that, we would need to see more code to help further.
When I Press the button to go to the next activity it returns me to the fist activity (not the desire one)
It means that your app crashes when loading your new activity, so it comes back the first one.
Check your activity layout, style configuration => clean your project => Run again.
Hope it can help.
I solved my problem by resizing the background images of the activities, and I added this extra attribute in the manifests file
<application
android:largeHeap="true" >
</application>

Main Activity Not Found Error in One Activity But not The other?

I have come across a strange error when i press a button that i supposed to return to teh main activity i get an activity not found error. But when i dod it from another activity, it works fine.
Working call:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Broken Call:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Campaign.this, Menu.class);
i.putExtra("Token", player.tokens);
// intent.putExtra("Round", player.round);
// intent.putExtra("Rank", player.rank);
// intent.putExtra("Score", player.score);
// intent.putExtra("Sec", player.secondsTapped);
// intent.putExtra("Min", player.minutesTapped);
// intent.putExtra("Hour", player.hoursTapped);
// intent.putExtra("Day", player.daysTapped);
// intent.putExtra("LifeTap", player.tapsInLife);
// intent.putExtra("SecTap", player.tapsPerSec);
// intent.putExtra("TapRo", player.tapps);
startActivity(i);
}
});
They are pretty much identical, but somehow the second one does not work. The first one has a bridging class before it. I looked through my xml file and nothing is wrong there since it works in one of my activities. The exact error i get is:
Unable to fin explicit activity class {com.tap.tapalot/android.view.Menu};
Thank You for Your help :)
I think you may have used wrong imports, so that Menu class is not the Menu-class you expect.
Try to specify the Menu.class in your intent with full package-path.
Check you seem to be importing. You are importing android.view.Menu in the second call. This is not your Activity class.
You are importing the wrong android.view.Menu class. You should import your Menu Activity class.
make sure you define 'Menu Class' in your mainfest file? like this:
<activity
android:name="pakgae.name.your.Menu"
android:label="#string/title_activity_menu" >
</activity>

Using intent in a public function

I've a method which is making a huge calculation and then calls an intent as follows
public void sampleMethod(final Context cont)
{
.
.
(huge calculation [50-80 lines])
.
.
Intent intent = new Intent(cont, TimesheetMain.class);
finish();
startActivity(intent);
}
This is present in Activity 'SampleActivity'. When I'm trying to access it through on object of Activity 'SampleActivity' from Activity 'B' as follows:
Context context = this;
SampleActivity sa = new SampleActivity();
sa.sampleMethod(context);
I'm getting a NullPointerException at the startActivity line of code while accessing it from Activity 'B'. I can't figure out where am i going wrong in here. Please help me out
EDIT 2
This seem to work when i added context to it like cont.startActivity(intent), but i need to know why shouldn't i use another class or another activity's function in a secondary class? Is the android framework is the reason? I've been doing this (without the intent part) for the past two months or so, i never faced any sudden force close issues in either emulator or in device(Nextbook professional 7 SE); Please explain it with a legit example
You're not supposed to create explicit instances of activities by yourself as you're doing like this:
SampleActivity sa = new SampleActivity();
Please provide a better description for your problem and what you want to achieve with the outcome of this issue.
try the follwng updated code:
public void sampleMethod(final Activity cont)
{
Intent intent = new Intent(cont, TimesheetMain.class);
cont.finish();
cont.startActivity(intent);
}
also move this method to a util class and call it from activity and pass the activity reference as follows
class ActivityB extends Activity
{
.
.
.
Util.sampleMethod(this);
}

Can you Parameterize an intent creation function?

In order to simplify some code, I'm wondering if it is possible to parameterize code that launches activities in an android application, so that instead of having 5
public void showSettings(View view) {
Intent SettingsActivity = new Intent(MainActivity.this, Settings.class);
startActivity(SettingsActivity);
I can do something like the following
public void showActivity(View view, String ActivityName) {
Intent ActivityName = new Intent(MainActivity.this, ActivityName.class);
startActivity(ActivityName);
Then, for each button in the UI, I simply apply the following to the "onclick" event
showActivity(Settings);
or
showActivity(domains);
This would save about 40-50 lines of code in my app. Obviously I know the above code is incorrect, but I'm not sure if it's possible to do what I'm trying to accomplish.
How about something like:
public <T> void showActivity(View view, Class<T> activity) {
Intent activityName = new Intent(MainActivity.this, activity);
startActivity(activityName);
}
You can invoke it with
showActivity(Settings.class);
I'd recommend use ACTIONs (String) instead of specifying exactly context and class. This way you even can share activities among applications, and if you decide to switch to different activity class, you can edit only android manifest, instead of editing all java source code calls this activity.

New Activity nullpointerexception

I have a beginners problem. Here is my situation:
I want to start a new activity from the main activity. The code to launch the new activity is found in a separate class file. I seem to be passing the wrong arguments and I am ending up in a nullpointerexception when trying to launch the new activity. The new activity launches fine when I place the code in the main activity class file, therefore the second activity and the manifest are fine. Here is a sample of my code:
In my main activity class where I instanciate the second class (THIS IS MY MAIN ACTIVITY. I OMITTED THE REST BECAUSE I DO NOT THINK IT IS RELATED TO THE PROBLEM):
Tester mytest = new Tester();
mytest.test(this);
In my second class file (THIS IS NOT AN ACTIVITY; IT IS A CLASS THAT IS INSTANTIATED IN THE ACTIVITY):
public class Tester extends Activity {
Intent myIntent;
public void test (Context context) {
myIntent = new Intent (Intent.ACTION_VIEW);
myIntent.setClass(context, newActivity.class);
thebutton.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
startActivity(myIntent);
}
}
):}
When I perform the click I receive a nullpointerexception at startactivity. Can anyone enlighten me on this please?I am sure that I am wrongly using the context.
Activities are started with Intents. Please read the Android Application Fundamentals first and try the Hello World app :)
I understood that you will use your separate Tester class at all cost ;) so I'm trying to adapt and help you out there.
First of all, don't let your class inherit from Activity. This won't help you, cause this calls will probably not have any valid context. Activity somehow implements the template pattern, providing you key method like onCreate(...), onPause(...) etc and is instantiated by the Android OS.
If you still want to use the class, you have to pass in the context. Probably you're aiming for some MVC/MVP pattern structure, anyway.
public class Tester {
private Context context;
public Tester(Context context){
this.context = context;
}
public void test () {
final Intent myIntent = new Intent(context, NewActivity.class);
//guess this comes from somewhere, hope through a findViewById method
thebutton.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
context.startActivity(myIntent);
}
}
)};
}
}
This would be a proposed solution from my side. A problem I still see here is on how you retrieve the button in that test() method. In order to have that work properly you have to retrieve it from some View class (with view.findViewByid(R.id.myButton)) or to create it dynamically and associate it with the view during the onCreate(...) of your Activity (probably using an Inflater).

Categories

Resources