Skip to content Skip to sidebar Skip to footer

Android: How To Use Classes In A Different File

I'm trying to make a handler class for many of my reusable methods. I'm having problems interfacing the separate class file. Can someone tell me what this example fails or what wo

Solution 1:

  1. Change the name myhandler to 'MyHandler', that sounds nice..
  2. In present situation you dont need to extend Activity class in MyHandler.
  3. You are setting text value twice delete following lines from testmethod they are not needed their..

TextView infotext = (TextView) findViewById(R.id.infotext);

    infotext.setText(innermessage); 

try and update your question if you get any error....

EDIT

//MyHandler.javapublicclassMyhandler {

        publicvoidtestMethod(TextView txtView){
            String innermessage = "This is a message from the innerclass.";            
            txtView.setText(innerMessage); 

        }

    }

// ClassExampleActivity.java

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

publicclassClassExampleActivityextendsActivity {


    TextView infotext;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Stringinfostring="Main Activity Class\n";
        TextViewinfotext= (TextView) findViewById(R.id.infotext);
        infotext.setText(infostring);

        Myhandlermh=newMyhandler();
        mh.testMethod(infoText);


    }
}

This should work!!

Solution 2:

I answer here, so you can see better my explanation.

In MyHandler class, you do the follow:

TextViewinfotext= (TextView) findViewById(R.id.infotext);

As you can see in the Android Reference:

public View findViewById (int id)

Since: API Level 1 Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle). Returns The view if found or null otherwise.

As I can understand there you haven't process any XML in MyHandler class, so you can't take the TextView from the XML and It is always null.

I hope that helps.

Solution 3:

Have you check if infotext is NULL?.

Solution 4:

Have you remembered to add both activities to your AndroidManifest.xml? That's caught me out on more than one or two occasions.

Solution 5:

I am able to work with classes .. in Android

because of you ..

Thank you very much for your help.

Comment 1:

You should replace .. myhandler .. with .. MyHandler

Comment 2:

You should copy-past .. the block .. (from AndroidManifest.xml)

<activityandroid:name=".ClassExampleActivity"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>

and replace ".ClassExampleActivity" with ".MyHandler"

to get

<activityandroid:name="pak.iceplanets.MainActivity"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name="pak.iceplanets.MyHandler"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>

Comment 3

ClassExampleActivity.java .. and .. Myhandler.java

must be in .. the same folder ..

(classexample .. from the folder .. apolo .. from the folder .. com)

You may replace .. ".MyHandler" .. with "com.apollo.classexample.MyHandler"

Comment 4 My files

MyHandler.java .. and .. MainActivity.java

are both .. in the folder .. iceplanets .. from the folder .. pak

Comment 5 The content .. of my file

MyHandler.java .. is below

package pak.iceplanets;

import android.app.Activity;

import android.widget.TextView;

public class MyHandler extends Activity {

publicvoidtestMethod(TextView txtView){

    String innerMessage = "This is a message from the innerclass.";            

    txtView.setText(innerMessage); 
}

}

Comment 6

Because I use Eclipse

I automatically .. write .. in the folder .. activity_main.xml

I obtained the lines

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"

    android:layout_centerHorizontal="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

Comment 7 To use (the class) MyHandler

I wrote (.. in MainActivity.java)

the lines ..

MyHandler mh;

TextView infoText;

after the line

public class MainActivity extends Activity {

...

I also wrote ( .. in onCreate(Bundle savedInstanceState){ )

the lines

// infoText .. is a reference to .. textView1       TextViewinfoText= (TextView) findViewById(R.id.textView1);      

 MyHandlermh=newMyHandler();

 mh.testMethod(infoText);

after .. the line .. setContentView(R.layout.activity_main);

.....

My pieces of the code .. I wrote above ..

works well .. on my computer.

Success

Best regards, Tonio

Post a Comment for "Android: How To Use Classes In A Different File"