title: Android: How to use classes in a different file
body: <p>I'm trying to make a handler class for many of my reusable methods.  I'm having problems interfacing the separate class file.</p>
  
  <p>Can someone tell me what this example fails or what would have to be done to make it work?</p>
  
  <p>ClassExampleActivity.java:
      package com.apollo.classexample;</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
  
  
  public class ClassExampleActivity extends Activity {
      /** Called when the activity is first created. */
  
      TextView infotext;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
  
          String infostring = "Main Activity Class\n";
  
          Myhandler mh = new Myhandler();
          infostring += mh.testmethod();
  
          // Trying to use the TextView widget from this MyHandler class
          // This works from the main class but fails in the MyHandler class
          TextView infotext = (TextView) findViewById(R.id.infotext);
          infotext.setText(infostring);
      }
  }
  </code></pre>
  
  <p>Myhandler.java:
      package com.apollo.classexample;</p>
  
  <pre><code>import android.widget.TextView;
  import android.app.Activity;
  
  public class Myhandler extends Activity {
  
      public String testmethod(){
          String innermessage = "This is a message from the innerclass\n";
          System.out.println(innermessage);
  
              /* This is an important component that I would like to use in this class
               Commenting these lines out and the application doesn't crash.
               I'm trying to learn how to use the next two lines in this MyHandler 
               class which I'm trying to make portable and reusable for other
               projectes.
              */
          TextView infotext = (TextView) findViewById(R.id.infotext);
              infotext.setText(innermessage); 
  
              // Placed to show that the MyHandler class is actually accessed
          return innermessage;
      }
  
  }
  </code></pre>
  
  <p>AndroidManifest.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.apollo.classexample"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk android:minSdkVersion="7" /&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:name=".ClassExampleActivity"
              android:label="@string/app_name" &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;activity android:name="MyHandler" /&gt;
          &lt;activity android:name="MyCRUDSQL" /&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>When calling the testmethod() the android app will crash.  If I comment it out it'll run and print the System.out.printlin() message to the LogCat.</p>
  
  <p>LogCat:</p>
  
  <pre><code>03-20 17:03:42.783: I/System.out(654): This is a message from the innerclass
  03-20 17:03:42.783: D/AndroidRuntime(654): Shutting down VM
  03-20 17:03:42.803: W/dalvikvm(654): threadid=1: thread exiting with uncaught exception (group=0x40014760)
  03-20 17:03:42.853: E/AndroidRuntime(654): FATAL EXCEPTION: main
  03-20 17:03:42.853: E/AndroidRuntime(654): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apollo.classexample/com.apollo.classexample.ClassExampleActivity}: java.lang.NullPointerException
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.os.Handler.dispatchMessage(Handler.java:99)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.os.Looper.loop(Looper.java:132)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.app.ActivityThread.main(ActivityThread.java:4025)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at java.lang.reflect.Method.invokeNative(Native Method)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at java.lang.reflect.Method.invoke(Method.java:491)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at dalvik.system.NativeStart.main(Native Method)
  03-20 17:03:42.853: E/AndroidRuntime(654): Caused by: java.lang.NullPointerException
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.app.Activity.findViewById(Activity.java:1744)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at com.apollo.classexample.MyHandler.testmethod(MyHandler.java:16)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at com.apollo.classexample.ClassExampleActivity.onCreate(ClassExampleActivity.java:23)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
  03-20 17:03:42.853: E/AndroidRuntime(654):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
  03-20 17:03:42.853: E/AndroidRuntime(654):  ... 11 more
  </code></pre>