我在一个Android项目中创建了扩展活动的主类,并创建了一个扩展主类的主类。我想在主类中使用在第二个类中声明的方法。但是我总是收到nullpointerexception
。我把两个简单的类放在这里。
主班:
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();
}
}
第二类:
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);
}
}
我看到错误是指行main_image =(ImageView)findViewById(R.id.main_image);但我不明白我在做什么错。有什么帮助吗?
这是日志猫:
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
谢谢
最佳答案
崩溃的原因是您创建了扩展了Utilities
的类AndroidServer
的对象,但未调用onCreate()
方法,因为它只是新的Java对象,并且未启动活动。因此,在initialize()
方法中,您可以找到视图,但是只有在开始该活动后才能这样做。希望你明白我的意思。
编辑:因此您无需在Utilities类中扩展Activity,并且可以进行如下操作:
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);
}