0

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

2 Answers 2

1

Your main_image is null, you need to declare your imageView in the same class where you declare setContentView(R.layout.activity_android_server);

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:

1
  • I have declared it now, but seems that isn't working. I have updated the code on my question. Thanks
    – Hieicker
    Commented Jul 12, 2013 at 10:00
0

The reason why it is crashing is that you create object of class Utilities which extends AndroidServer but onCreate() method is not called because it is just new java object and activity is not started. So in initialize() method you are finding view but you can't do that until you start that activity. Hope you understand what I mean.

EDIT: so you don't need to extend Activity in Utilities class and you can make something like this:

public class Utilities {

   public void initialize(View mainHolder){
      ImageView imageView = mainHolder.findViewById(R.id.main_image);
      imageView.setImageResource(R.drawable.image3);
   }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   LayoutInflater inflater = LayoutInflater.from(this);
   View contentView = infler.inflate(R.layout.activity_android_server, null);
   setContentView(contentView);
   this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

   Utilities utilities = new Utilities();
   utilities.initialize(contentView);
}
5
  • Ok thanks. So has more sense put the call to the utilities method in the onResume() method of the activity? Seems that is isn't working too...
    – Hieicker
    Commented Jul 12, 2013 at 10:06
  • Like I see now, your code is absolutely weird and I really don't understand what you want to achieve, because I overlooked in line main_image.findViewById(R.id.main_image); that you are finding view in ImageView. And main_image is always null.
    – Koso
    Commented Jul 12, 2013 at 10:07
  • the code is simple because i'm only try to learn how call methods from other classes. I have add this line now: main_image.setImageResource(R.drawable.image3); I want show the image3 object (is a simple .jpg image) in the main view, but i want do it from another class and after call only the method in the main activity.
    – Hieicker
    Commented Jul 12, 2013 at 10:14
  • I edited my answer. You really should learn some java and android basics using some tutorials etc.
    – Koso
    Commented Jul 12, 2013 at 10:24
  • Thanks it is working, and yes i'm learning, studying and trying.
    – Hieicker
    Commented Jul 12, 2013 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.