Simple activity switch not working. No Errors - android

I have a basic calculator app I'm making. Two activities, the main one and ResultView.
I've made it where I click a button on activity A to go to activity B. The log says activity B is started and "displayed" successfully, the title for the new activity loads, but the body does NOT show. I added a simple Text view with static text.. see the result.xml at the bottom. I also tried inserting information programmatically, but that didn't do.
When I debug the program, I tried putting breakpoints as the activity is called with startActivity() as well as on the first line of the onCreate method within the ResultView class (my activity "B") but the program never hits the second breakpoint. In fact, it looks as if Looper.class is called in the end.
This bit of code is placed in the button handler on acitivity A:
i.putExtra("test1",34);
i.putExtra("test2",35);
this.startActivity(i);
This in the onCreate function in activity B:
public void OnCreate(Bundle
savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
The activity is in the manifest, within the "application" tag:
<activity
android:name="ResultView"></activity>
If I didn't supply enough info, let me know.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llParent"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="HELLO WORLD"
/> </LinearLayout>
If more info is needed, let me know...in short, "HELLO WORLD" does not display at all.

It's not OnCreate, it's onCreate (lowercase o). Otherwise the method won't be overriden. The #override annotation has no effect if it's omitted, it's just for readability for the programmer.

Are you sure that the public void line or the line before that contains #Override? If not, you're not overriding the OnCreate method. The code should read
#Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
EDIT
Of course the "O" must not be a capital "O"...

Related

It is not working, and Right place to implement onSaveInstanceStance()

I am trying to grasp the concept of life cycle call back methods of an activity and what happens inside them.
I am writing a small activity class which just displays a text.
The Problem is that the activity starts to display and i get a dialog saying "unfortunately Activity Lifecycle and Coordination has stopped working"
Please tell me how to fix this.
The code is as follows:-
package com.practice.lifecycle_coordination;
//import android.app.ActionBar;
import android.app.Activity;
//import android.os.Build;
import android.os.Bundle;
class MainActivity extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onStart(){
super.onStart();
}
protected void onResume(){
super.onResume();
}
protected void onPause(){
super.onPause();
}
protected void onStop(){
super.onStop();
}
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
}
}
LAYOUT FILE:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/title"
android:gravity="center_horizontal"
android:textStyle="bold" />
</LinearLayout>
STRINGS RESOURCE FILE:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity LifeCycle</string>
<string name="action_settings">Settings</string>
<string name="title">MAIN ACTIVITY</string>
</resources>
Is onSaveInstanceState() implemented at the right place? In case I want to save some additional data into the bundle, is this the right place to implement it?
I know that I don't need to write any life cycle call back methods except onCreate(). But I just wanted to check.
You don't have to write onCreate() actually. The only life cycle methods you have to write are ones in which you need to override and implement your own code within. onCreate() is overwritten in virtually every Activity, however, because you typically are going to want to use this point in the life cycle to initialize variables etc.
As for Is onSaveInstanceState() implemented at the right place?
Yes. The order within your Activity in which your life cycle methods occur doesn't matter in the slightest. As long as you're in the right activity, you're fine. These methods are called automatically as your activity goes through those life cycle stages.
As for what's actually wrong with the current code? We'll need a lot more information. Your logcat will give you some information on what actually caused the crash. Post that, and your problem can be better diagnosed.

Using fragments as workers for activity

I'm trying to learn how to use a fragment as a worker for an android activity. I have the following simple xml layout for my main activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press me" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
I define my fragment using the following class definition:
public class UpdateTextFragment extends Fragment {
public static UpdateTextFragment newInstance() {
return new UpdateTextFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void startUpdateText() {
TextView textView = ((TextView) getActivity().findViewById(R.id.text));
textView.setText("I've been pressed!");
}
}
Then from my main activity I simply add the fragment and call the startUpdateText method of the fragment using the button's onClickListener , i.e.,
public void onClick(View arg0) {
UpdateTextFragment fragment = UpdateTextFragment.newInstance();
getFragmentManager().beginTransaction().add(fragment, "updateText").commit();
fragment.startUpdateText();
}
The code compiles and uploads to a tablet with no problems. I would expect it write the text "I've been pressed!" to the text view when the button is pressed, but the app just crashes with the standard "Unfortunately app has stopped working". I haven't implemented a class to catch this uncaught exception yet - I was hoping that it may be something obvious I'm missing or don't understand?
Thanx
Altough this is an old question I'd like to answer it as I was puzzled why your sample code didn't work.
The reason you get a NullPointerException is that you instantiate your Fragment and immediately call a method that requires to have the activity injected into the Fragment. The activity is injected by the FragmentManager / FragmentTransaction#commit method BUT this method does not evaluate the transaction immediately (as from the JavaDocs):
Schedules a commit of this transaction. The commit does
not happen immediately; it will be scheduled as work on the main thread
to be done the next time that thread is ready.
Which means
getFragmentManager().beginTransaction().add(fragment, "updateText").commit();
fragment.startUpdateText();
will yield the NPE in startUpdateText() (as the transaction was not executed yet!).
By adding the method call getFragmentManager().executePendingTransactions(); right after the commit the transaction will be performed immediately and the activity injected into the fragment. getActivity() in the Fragment now returns the Activity it was attached to and your sample works :)
As from the comments below your question: It is true that A Fragment is a re-usable 'UI-element' hosted inside an Activity. (#Stefan de Bruijn) but also that [...] a fragment is not required to be a part of the activity layout; you may also use a fragment without its own UI as an invisible worker for the activity. (as the official Android doc says).
So a Fragment is not necessarily a GUI component (i.e. the view in MVC) but also acts as a controller with its own lifetime/lifecycle.

changes in main.xml are not reflected in the android app in netbeans

I am new to android apps. I am using the netbeans 7.0.1 IDE to develop android apps. I have written the following code in the main java file:
package com.test.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class helloworld extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t1=new TextView(this);
t1.setText("hello world..!!!!");
setContentView(t1);
}
}
This was working fine.
I edited the main.xml file to display a textfield and button as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"/>
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"/>
</LinearLayout>
Of course I have added all the corresponding strings in strings.xml. But when I try to run my app these weren't displaying... :( . I mean the same string that was displayed previously was being displayed.
Can anybody figure out what is the mistake??
Remove below lines from onCreate method of your activity
TextView t1=new TextView(this);
t1.setText("hello world..!!!!");
setContentView(t1);
Because you are setting the contentView from the main.xml and then again you are creating the TextView dynamic and setting that TextView as a contentView. so you are getting the static string "hello world..!!!!"
Edit
There is a spelling mistake in your android:oreintation line which you are using for set orientation. use "android:orientation" instead.
remove setContentView(t1); from your code
public void setContentView (int layoutResID)
Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
public void setContentView (View view)
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy.
from developer.android.com
Since you called it two times, the second call did override the first view.
Right Clicck your project and clean it ..
uninstall your app from device or emulator.....
then again reinstall .....
i hope it will work my friend

Using GlSurfaceview in activity

I have an Activity and i had set activity's content view as "R.layout.main.xml".And i have an another class which contains animation created using openGL. Now i need to use this animation in the background of the Activity.
The code is like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pixie);
mGLView = new ClearGLSurfaceView(this);
setContentView(mGLView);
}
But my app is Crashing ..How can i solve this.
When you call the setContentView() a second time, you replace what had been set the first time, leaving you with only the background. The crash is most likely because you depend on the elements in the main layout, which is removed.
Rather than calling setContentView() twice, you should include the GLSurfaceView in the main layout. Below is an example of how this can be done:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent>
<your.application.package.ClearGLSurfaceView
android:layout_width="match_parent"
android:layout_width="match_parent"/>
<!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie-->
</FrameLayout>
Then you can load this layout in your onCreate() as usual (main_pixie_new refers to the above xml, I just gave it that name to keep things as clear as possible):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pixie_new);
}

How can I access views in activities within a TabActivity?

I've got a TabActivity containing other activities intended to split up a form. The TabActivity has in its layout a button intended to collect the data from all the form-related views across all the activities contained within the TabActivity and store it. The problem I'm running into is that the TabActivity doesn't appear to have access to these views; when I call findViewById() with one of them, I get a NullPointerException.
The documentation seems sparse about exactly how TabActivity works with respect to controlling the activities it contains. If it destroys an activity when switching from it to a different one, the situation I'm in would make sense. I'd like to know the best approach for accomplishing the goal described above.
src/com/vendor/MyTabActivity.java:
public class MyTabActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_tab_activity);
final Button saveButton = (Button) findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// NullPointerException happens here
String fieldValue = ((TextView) findViewById(R.id.text_field)).getText().toString();
}
});
}
}
res/layout/my_tab_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost">
<LinearLayout>
<TabWidget android:id="#android:id/tabs"/>
<FrameLayout android:id="#android:id/tabcontent" />
<Button android:id="#+id/save_button"/>
</LinearLayout>
</TabHost>
src/com/vendor/NestedActivity.java:
public class NestedActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nested_activity);
}
}
res/layout/nested_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout>
<EditText android:id="#+id/text_field"/>
</RelativeLayout>
</ScrollView>
Your problem comes from these two lines in MyTabActivity...
setContentView(R.layout.my_tab_activity);
...and...
String fieldValue = ((TextView) findViewById(R.id.text_field)).getText().toString();
...although you obviously know the findViewById(R.id.text_field) is what's causing it.
Using findViewById(...) only works when trying to access UI elements which have been inflated as part of your current Activity. As there isn't a TextView with the resource id of R.id.text_field in the my_tab_activity.xml, it's never going to work.
Accessing activities which are tab content from the TabHost / TabActivity is tricky. My suggestion would be to use SharedPreferences which can be accessed from everywhere in your app. Once a TextView (or any other user-input item) is changed, save it to a SharedPreferences using a 'key' which identifies which activity/tab it came from. From then on, the TabActivity can collate the data easily.
You can get a reference to activities running inside of the tab activity using getLocalActivityManager() or getCurrentActivity(). For the activity object you get back you can do activity.findViewById() to get a reference to a view inside of the specific activity. But to point out TabActivity has been deprecated and you should be using Fragments to do what you are looking for. If you are targeting a version of Android earlier than 3.0 you can use the compatibility library to access fragments.

Categories

Resources