Android Java: Global variables through subclass; can't launch app?
<p>I've been reading up examples on how to do this, trying to piece together a functioning example from posts on this site and others.  I'm sure I'm missing something small, but as a novice to Java, I could use some assistance.</p>

<p>I'm merely trying to create a small example, derived from the automated hello world generated when creating a new project in Eclipse.  All I want to do is be able to store a few global variables in a subclass, then reference those values in my main activity.  Unfortunately, every time I try to run the app, it crashes "Unfortunately, GeneralTest1 has stopped", and the log cat error is not very helpful.</p>

<p>Quick overview:</p>

<ul>
<li>GlobalVars class extends Application</li>
<li>In the manifest, android:name has been added to reference the additional GlobalVars class</li>
<li>Within my main activity, I'm initializing the global vars class with getApplicationContext()</li>
</ul>

<p>Here is everything I've got; any help would be much appreciated!</p>

<p><strong>MainActivity.java</strong></p>

<pre><code>package com.example.generaltest1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    GlobalVars myVars = ((GlobalVars)getApplicationContext());      
    TextView myText = (TextView) findViewById(R.id.myText);


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myText.setText(myVars.getMyString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
</code></pre>

<p><strong>GlobalVars.java</strong></p>

<pre><code>package com.example.generaltest1;

import android.app.Application;

public class GlobalVars extends Application {

    String myString = "Some Text";

    public String getMyString() {
        return myString;
    }

    public String setMyString(String string) {
        this.myString = string;
        return myString;
    }
}
</code></pre>

<p><strong>activity_main.xml</strong></p>

<pre><code>&lt;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" &gt;

    &lt;TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" /&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p><strong>strings.xml</strong></p>

<pre><code>&lt;resources&gt;

    &lt;string name="app_name"&gt;GeneralTest1&lt;/string&gt;
    &lt;string name="hello_world"&gt;Hello world!&lt;/string&gt;
    &lt;string name="menu_settings"&gt;Settings&lt;/string&gt;
    &lt;string name="title_activity_main"&gt;MainActivity&lt;/string&gt;

&lt;/resources&gt;
</code></pre>

<p><strong>GeneralTest1 Manifest</strong></p>

<pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.generaltest1"
    android:versionCode="1"
    android:versionName="1.0" &gt;

    &lt;uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" /&gt;

    &lt;application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:name="GlobalVars"&gt;
        &lt;activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>

<p><strong>Log Cat</strong></p>

<pre><code>08-14 09:53:05.546: E/Trace(620): error opening trace file: No such file or directory (2)
08-14 09:53:05.656: D/AndroidRuntime(620): Shutting down VM
08-14 09:53:05.656: W/dalvikvm(620): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-14 09:53:05.676: E/AndroidRuntime(620): FATAL EXCEPTION: main
08-14 09:53:05.676: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.generaltest1/com.example.generaltest1.MainActivity}: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.os.Looper.loop(Looper.java:137)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 09:53:05.676: E/AndroidRuntime(620):  at java.lang.reflect.Method.invokeNative(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620):  at java.lang.reflect.Method.invoke(Method.java:511)
08-14 09:53:05.676: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 09:53:05.676: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 09:53:05.676: E/AndroidRuntime(620):  at dalvik.system.NativeStart.main(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): Caused by: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-14 09:53:05.676: E/AndroidRuntime(620):  at com.example.generaltest1.MainActivity.&lt;init&gt;(MainActivity.java:10)
08-14 09:53:05.676: E/AndroidRuntime(620):  at java.lang.Class.newInstanceImpl(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620):  at java.lang.Class.newInstance(Class.java:1319)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-14 09:53:05.676: E/AndroidRuntime(620):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-14 09:53:05.676: E/AndroidRuntime(620):  ... 11 more
</code></pre>
