oncreate function doesnt start - android

Trying to launch another activity from my main activity using a button i placed on my main activity, now when the button is clicked it prints "call activity" and so it works fine, but when the second activity is launched it seems like it doesn't execute oncreate method, as it doesn't print "start" and doesn't call GetCar(); method. The only thing it does is to show the xml.
button that launches second activity (print "call activity" is working fine)
public void allCars(View v){
setContentView(R.layout.activity_cars_menu);
System.out.println("call activity");
}
second activity code (doesn't print "start" or call GetCar().)
public class CarsMenu extends Activity {
Button btn;
public static String[] carId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cars_menu);
System.out.println("start");
GetCar();
}
}
second activity xml (can't see viewlist probably because i can't set text (doesn't call GetCar()) .
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
all of my activity is on the manifest and i'm not getting any errors on logcat.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="btnCarClick"
android:text="Refresh" />
<ListView
android:id="#+id/MlistView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">
</ListView>
</RelativeLayout>

You might want to start your Activity:
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(myIntent);
Just setting the content view is not enough

Your starting your second Activity in the wrong way, and you only change your view in your first Activity.
In order to change activity you need to call
startActivity (Intent intent)
for more info you can read on
http://developer.android.com/training/basics/firstapp/starting-activity.html

Actually you are just changing the content UI of current activity. As
setContentView(R.layout.activity_cars_menu);
does only that. In real your 2nd activity is never started even. For starting second activity you should do
Intent i = new Intent (Activity1.this, CarsMenu.class);
startActivity (i);
Make sure you have added CarsMenu activity to your Android Manifest.

Related

In android, How to resume previous activity , on button click from second activity

I have a form to be filled by user and next button "Next" in Activity1.
When user clicks "Next" button second activity Activity2 is started.
In Activity2 i have previous button "Previous".(* not device back button)
So when user clicks " Previous" button, Activity1 should be opened with the entered details in the form.
Activity1 should not be refreshed.
Seached alot on stackoverflow but no luck..!!!
Why don't you use startActivityForResults and then in the started activity finish()
In the started activity you have access to your intent in the onCreate method getIntent(); then you can use setResult(Activity.RESULT_OK, result); or setResult(Activity.RESULT_OK); (For canceled Activity.RESULT_CANCELED) to return result code and data and after you set the result you call finish and then return (code doesn't exit the methods if I remember correctly).
Then in the first activity you get the result and handle what to do with it in:
onActivityResult(int requestCode, int resultCode, Intent data)
Another option:
You could also use the logic for back button pressed calling the method from your code: super.onBackPressed();
Edit
As I promised here's an Example
Two activities - first one have two TextViews and a button next that launches the second activity -> in the second activity two EditTexts in which you enter some data which is then returned to the first activity when you press previous button. If you press back button instead of previous button, you enter in the canceled logic which is not doing anything in the example there is only a comment.
MainActivity
TextView textViewFirstName;
TextView textViewFamilyName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewFirstName = (TextView)findViewById(R.id.first_name_edit_text);
textViewFamilyName = (TextView)findViewById(R.id.family_name_edit_text);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//requestCode here is 12345 that we passed when we started SecondActivity
if(resultCode == Activity.RESULT_OK){
String resultFirstName = data.getStringExtra("firstName");
String resultFamilyName = data.getStringExtra("familyName");
textViewFirstName.setText(resultFirstName);
textViewFamilyName.setText(resultFamilyName);
}
if (resultCode == Activity.RESULT_CANCELED) {
//Canceled logic
}
}
public void nextButtonClick(View view) {
Intent i = new Intent(view.getContext(), SecondActivity.class);
//If you want you could pass some additional data, like which action to take
//if you're reusing the second activity for more than one use case
i.putExtra("someAdditionalData", "Some string that you want to pass");
//12345 is int that you pass and will be returned as requestCode in onActivityResult
startActivityForResult(i, 12345);
}
SecondActivity
EditText editTextFirstName;
EditText editTextFamilyName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editTextFirstName = (EditText)findViewById(R.id.first_name_edit_text);
editTextFamilyName = (EditText)findViewById(R.id.family_name_edit_text);
Bundle bundle = getIntent().getExtras();
String someAdditionalData = bundle.getString("someAdditionalData");
}
public void previousButtonClick(View view) {
Intent returnIntent = new Intent();
returnIntent.putExtra("firstName", editTextFirstName.getText().toString());
returnIntent.putExtra("familyName", editTextFamilyName.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:text="Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:hint="First name"
android:id="#+id/first_name_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:hint="Family name"
android:id="#+id/family_name_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:onClick="nextButtonClick"
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
second_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity">
<TextView
android:text="Second"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:hint="First name"
android:id="#+id/first_name_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:hint="Family name"
android:id="#+id/family_name_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:onClick="previousButtonClick"
android:text="Previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
On previousButton onClickListener you can call finish();
This will close the current activity and reload the previous one from the stack. This is a quick hack.
Try overriding onBackPressed method inside second activity and call it on click event of previous button
Inside Activity 2
#Override
public void onBackPressed()
{
super.onBackPressed();
}
And call this on previous button click event
buttonPrevious.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
I would refrain from using finish(); cuz it kills the activity from where it's called. Try to use:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
instead of finish();

Passing data from an Activity to a Layout in Android

I have an Android Activity as below.
public class DummyActivity extends android.support.v7.app.ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy_layout);
Intent intent = getIntent();
int dots = intent.getExtras().getInt("dots");
}
}
This activity gets the value of dots from another activity without any problem.
Now I want to pass dots to the layout dummy_layout which is given below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.test.DummyView
android:id="#+id/dummyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
I tried creating a hidden field in 'dummy_layout' but could not get it working. Is there any straight forward way I can do this ?. I need to access the value of 'dots' in the init method of 'dummyView' which is the class handling 'dummy_layout' as seen in the layout xml.
Why don't you just do
((DummyView) findViewById(R.id.dummyView)).setDots(dots)
in your onCreate?

Not able to set source of an imageview in Included layout in an activity

I have created two XML layout and Activity respecively.
A XML file A contains a layout with Image view Pointed to Activity A.
A XML file B, in that i included XML A and made Activity B extends Acitivity A
In Acivity A oncreate i set the image source for the XML file A. but its not setting the image source and not getting any error also. Plz help me
Here is my code
Xml A
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MasterBaseLayOut"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
XML B
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="#+id/Master"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/xmlA" />
<Button
android:id="#+id/btn1"
android:onClick="btn1_onclick"
android:text="#string/title_1"/>
</RelativeLayout>
Activity A
public class ActivityA extends Activity{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.XMLA);
ImageButton img1 =(ImageButton)findViewById(R.id.imagebutton);
img1.setBackgroundResource(R.drawable.imgtest);
}
}
Activity B
public class ActivityB extends ActivityA {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.XmlB);
}
}
Tough you have no code, but i assume you are not getting the imageView in Activity B.So in Activity
B's onCreate() method call the Activity A's onCreate using super keyword,most probably you will get the imageview with source image.
public void onCreate(Bundle b) {
super.onCreate(b);
}
This is what you have:
ActivityA extends ActivityB
This is what it should be:
ActivityB extends ActivityA
Also, you should consider pressing "Ctrl + Shift + F" to format your code, just a little tip.

How to make a button redirect to another XML layout

I'm making a button in xml(res / layout / activity_home.xml), like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity" >
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/schkopwide"
android:contentDescription="#string/HTI"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="Intent i = new Intent(activity_store.xml);
startActivity(i);"
android:text="#string/HTI" />
</RelativeLayout>
so what should I add into this xml to let it redirect to another xml page (res / layout / activity_store.xml)?
Thank you
If you Want to show two different layouts in Same Activity, then ViewSwitcher is best layout.
You can add multiple layouts in ViewSwithcher. And Replace them by using viewswitcher.next(); function.
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Add Two View’s Here -- >
</ViewSwitcher>
You can take reference from this link: http://abhiandroid.com/ui/viewswitcher
Try out as below:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="start"
android:text="#string/HTI" />
In your main activity :
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
Here is your ActivityStore Class code:
public class ActivityStore extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
}
Also add the activity into your mainfest file.
<activity
android:name=".ActivityStore"
android:label="#string/app_name"/ >
You can't add the launch of an Intent inside the onclick parameter in XML. You have to do it by code.
In your code:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
And in the OnCreate of the ActivityStore class, put this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
NOTE: I supposed that yous activity_store class is called ActivityStore
You need to check on Android documentation :
Activity
OnClickListener
Intent
http://developer.android.com/training/index.html
Good luck.
Try this,
Statically include XML layouts inside other XML layouts. use include. Add the below code in your activity_store.xml
<include layout="#layout/activity_home"/>
Sure you will get solution.
A simple way would be to create an Activity with another xml attached to it and then use intent.

Android SetText Null pointer Exception

I've just begin Android development and I'm sure you can help with this (I'm sorry about my bad English)
I have a main activity, and at a certain moment i want to call another activity, in wich a want to change a textview with some message. At this moment I get a Null pointer Exception if I dont put
setContentView(R.layout.register);
But when I put that line, I see for a milisecond correctly the Register activiy with my new text "Android2" and then jump again to a register activity with no text. I mean I draw it twice.
I hope I have explained enough.
The question is Where do I have to put setcontentview and with what layaout.
Thank you very much, Daniel
I show you some code:
My main activity has this method:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
Intent i = new Intent(this, register.class);
startActivity(i);
setContentView(R.layout.register);
TextView text = (TextView) findViewById(R.id.texto);
try {
text.setText("Android2");
}catch(Exception e) {
Log.i("Log", e.getMessage()+"Error!"); // LogCat message
}
}
//super.onActivityResult(requestCode, resultCode, data);
}
My second activity class called register:
public class register extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
}
The register Interface is register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/register" />
<TextView
android:id="#+id/texto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/continue_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/save" />
<Button
android:id="#+id/new_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/repeat" />
</LinearLayout>
</LinearLayout>
What you basically do is the following:
you prepare an intent to start another activity
you start the activity you prepared the intent for
you set the current activity's content to R.layout.register
you get the textView (on the current activity)
you set the text of the textView to Android2
And, at this moment the new activity appears on the screen. Please note that your code is not correct, since you manipulate UI elements in the current activity and you expect changes on the newly started activity.
Move this code
TextView text = (TextView) findViewById(R.id.texto);
try {
text.setText("Android2");
}catch(Exception e) {
Log.i("Log", e.getMessage()+"Error!"); // LogCat message
}
to the register activity's onCreate() method.
BTW, usually, when you create a class, it's name should begin with a capital letter according to the standards.
You have two different activities. One of them is using the register.xml view, and the second one is trying to access the register view. The view only exists in your "register" activity. The other activity seems to have no view? That's probably why you're getting NULL.
You should merge those two classes together, since it looks like you're trying to access texto from the same view.
So, to summarise, findViewById should be called from within the activity that calls setContentView.
Remove this line and it should work
startActivity(i);
Not sure why you are calling that as an external activity.
Otherwise move below code to your register class
setContentView(R.layout.register);
TextView text = (TextView) findViewById(R.id.texto);
try {
text.setText("Android2");
}catch(Exception e) {
Log.i("Log", e.getMessage()+"Error!"); // LogCat message
}
}

Categories

Resources