I have created in an Android project the main class that extend the activity, and another one that extend the main class. I want to use in the main class a method declared in the second class. But i am receiving always a nullpointerexception
. I put here the two simple classes.
The main class:
package com.example.androidserverapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.Menu;
import android.widget.ImageView;
public class AndroidServer extends Activity {
public ImageView main_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_server);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Utilities utilities = new Utilities();
utilities.initialize();
}
}
The second class:
package com.example.androidserverapp;
import android.widget.ImageView;;
public class Utilities extends AndroidServer{
public ImageView main_image;
public void initialize(){
main_image.findViewById(R.id.main_image);
main_image.setImageResource(R.drawable.image3);
}
}
I saw that the error is referring to the line main_image = (ImageView) findViewById(R.id.main_image); but i'm not understanding what i'm doing wrong. Any help? Here there is the log cat:
07-12 11:35:04.473: E/AndroidRuntime(14954): FATAL EXCEPTION: main
07-12 11:35:04.473: E/AndroidRuntime(14954): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidserverapp/com.example.androidserverapp.AndroidServer}: java.lang.NullPointerException
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.ActivityThread.access$600(ActivityThread.java:151)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.os.Looper.loop(Looper.java:155)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.ActivityThread.main(ActivityThread.java:5454)
07-12 11:35:04.473: E/AndroidRuntime(14954): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 11:35:04.473: E/AndroidRuntime(14954): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 11:35:04.473: E/AndroidRuntime(14954): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
07-12 11:35:04.473: E/AndroidRuntime(14954): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
07-12 11:35:04.473: E/AndroidRuntime(14954): at dalvik.system.NativeStart.main(Native Method)
07-12 11:35:04.473: E/AndroidRuntime(14954): Caused by: java.lang.NullPointerException
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.Activity.findViewById(Activity.java:1870)
07-12 11:35:04.473: E/AndroidRuntime(14954): at com.example.androidserverapp.Utilities.initialize(Utilities.java:8)
07-12 11:35:04.473: E/AndroidRuntime(14954): at com.example.androidserverapp.AndroidServer.onCreate(AndroidServer.java:20)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.Activity.performCreate(Activity.java:5066)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
07-12 11:35:04.473: E/AndroidRuntime(14954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
07-12 11:35:04.473: E/AndroidRuntime(14954): ... 11 more
Thanks