this.getApplication() returns NULL pointer

by Stefan » Sat, 10 Oct 2009 01:49:05 GMT


Sponsored Links
 I'm running the following line in an Activity, which is within the
same application, but in a different package:
AppObject appObj = (AppObject)this.getApplication();  // FYI:
AppObject is my extension class of Application.

It returns only a null pointer, while when I move it to the "main"
package and run it from there it returns the application reference as
expected.

I've defined the activity in my AndroidManifest.xml  with the full
qualified class name, since it is in another package: <activity
android:name="com.foo.bar.TestActivity"></activity>

Any ideas of what I've to do different?

Thanks

--~--~---------~--~----~------------~-------~--~----~



this.getApplication() returns NULL pointer

by Dianne Hackborn » Sat, 10 Oct 2009 02:27:18 GMT


 Are you calling it in the constructor?  The activity is not yet fully
initialized at that point.






-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


this.getApplication() returns NULL pointer

by Stefan » Thu, 15 Oct 2009 17:32:32 GMT


 Nope. Right now, for testing purposes, it is triggered through a
button click.

I've also included android:name="AppObject" within the <application>-
tag of the Manifest. And the initialization of the AppObject seems to
work properly.

The only thing that doesn't work is calling AppObject appObj =
(AppObject)this.getApplication(); from an Activity that is not in the
same package as the AppObject (which is in the "main" package). As
soon as I move this Activity into the "main" package it works properly
and (AppObject)this.getApplication(); returns the Application Object.






--~--~---------~--~----~------------~-------~--~----~



this.getApplication() returns NULL pointer

by jotobjects » Fri, 16 Oct 2009 02:53:56 GMT


 Does "this" refer to the OnClickListener button listener object?
Often this is an inner member class of the activity so it would work
if you take the "this." off.  But you seem to have something a little
more complicated going on.  Maybe we need to see the OnClickListener
code.






--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Using Preferences to login?

Hi,
I want to use Preferences to get data in EditText to login my app. So
I can get data in EditText but I can't compare the data of EditText in
file Login and in file Preferences. Would you tell me how I can do?
Preferences file :
XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/
android">
     <PreferenceCategory android:title="UserName">
          <EditTextPreference
               android:key="cusername"
               android:title="Change UserName"
               android:summary="Click to pop up a field for entry"
               android:dialogTitle="Enter new User"/>
     </PreferenceCategory>
     <PreferenceCategory android:title="Password">
          <EditTextPreference
               android:key="cpassword"
               android:title="Change Password"
               android:summary="Click to pop up a field for entry"
               android:password="true"
               android:dialogTitle="Enter new Password"/>
     </PreferenceCategory>
</PreferenceScreen>

Java:
Java:

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class ChangeUser extends PreferenceActivity {
     @Override
      protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           addPreferencesFromResource(R.xml.changeuser);
     }
}


This is my Login file:
Java:

public class Login extends Activity  {
        private TextView tempUser;
     private TextView tempPass;

     private EditText eUser;
     private EditText ePass;
     private Button mLog;
     private Button mClose;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        eUser = (EditText) findViewById(R.id.username);
        ePass = (EditText) findViewById(R.id.password);

        SharedPreferences
prefs=PreferenceManager.getDefaultSharedPreferences(this);
        tempUser.setText(prefs.getString("cusername", ""));
        tempPass.setText(prefs.getString("cpassword", ""));

        mLog = (Button) findViewById(R.id.loginbutton);
        mLog.setOnClickListener (mLogListener);
        mClose = (Button) findViewById(R.id.closebutton);
        mClose.setOnClickListener (mCloListener);
}
 private OnClickListener mLogListener = new OnClickListener()
{
     public void onClick(View v) {

     }
    };

    private OnClickListener mCloListener = new OnClickListener() {
     public void onClick(View v) {
          finish();
     }
    };
}

Login.xml
XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
     android:layout_width="fill_parent"
        android:layout_height="fill_parent">
<ScrollView
     android:layout_width="fill_parent"
        android:layout_height="fill_parent">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:scrollbars="vertical">
     <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center_vertical|center_horizontal|
center"/>
     <TextView
         android:id="@+id/logintext"
         android:layout_height="wrap_content"
         android:text="TEST"
          android:textStyle="bold"
          android:textSize="20px" android:layout_gravity="center"
android:layout_width="fill_parent"/>
     <TextView
          android:layout_width="fill_parent"
          android:layout_height="20px"
          android:layout_gravity="center_vertical|center_horizontal|
center"/>
     <TextView
         android:id="@+id/usertext"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center_vertical|center_horizontal|
center"
         android:text="USERNAME"/>
    <EditText
         android:id="@+id/username"
         android:layout_height="wrap_content"
          android:focusableInTouchMode="true"
          android:layout_gravity="center_horizontal"
          android:layout_marginRight="5dip"
          android:layout_marginLeft="5dip"
          android:layout_width="320dip" android:singleLine="true"
android:focusable="true"/>
     <TextView
          android:layout_width="fill_parent"
          android:layout_height="10px"
          android:layout_gravity="center_vertical|center_horizontal|
center"/>
     <TextView
         android:id="@+id/passtext"
         android:text="PASSWORD"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center_vertical|center_horizontal|
center"/>
    <EditText
         android:id="@+id/password"
         android:layout_height="wrap_content"
          android:focusableInTouchMode="true"
          android:layout_gravity="center_horizontal"
          android:layout_marginRight="5px"
          android:layout_marginLeft="5dip"
          android:layout_width="320dip"
          android:password="true" android:singleLine="true"/>
     <TextView
          android:layout_width="fill_parent"
          android:layout_height="30px"
          android:layout_gravity="center_vertical|center_horizontal|
center"/>
<LinearLayout
     android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_width="wrap_content">
     <Button
         android:id="@+id/loginbutton"
         android:layout_height="50dip"
          android:text="LOGIN"
          android:layout_width="103dip"/>
     <Button
          android:id="@+id/closebutton"
          android:layout_height="50dip"
          android:text="CLOSE"
          android:layout_width="103dip"/>
</LinearLayout>
     <TextView
          android:layout_width="fill_parent"
          android:layout_height="30px"
          android:layout_gravity="center_vertical|center_horizontal|
center"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>


Thanks,

--Alex
--~--~---------~--~----~------------~-------~--~----~

2. Android Killswitch

I am looking for any information on the Android Kill switch ?
Is this a application ? is it a service ? is it part of Cupcake ?
What
is the path in the source code  for kill switch ?
Please help
--~--~---------~--~----~------------~-------~--~----~

3. Doesn't Android-1.0 support Recording Video well?

4. How to communicate between 2 activities

5. How to use two different icon in two different themes

6. restore savedInstance

7. how to send mail in SDK 1.5