gpt4 book ai didi

java - Android/Java - 来自类的通信返回 java.lang.nullpointerException

转载 作者:行者123 更新时间:2023-12-01 14:20:32 26 4
gpt4 key购买 nike

我在 Android 项目中创建了扩展 activity主类,以及另一个扩展主类的类。我想在主类中使用在第二个类中声明的方法。但我总是收到 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

谢谢

最佳答案

您的main_image为空,您需要在声明setContentView(R.layout.activity_android_server);的同一类中声明您的imageView;

引起:java.lang.NullPointerException 07-12 11:35:04.473: E/AndroidRuntime(14954): 在 android.app.Activity.findViewById(Activity.java:1870) 07-12 11:35:04.473 : E/AndroidRuntime(14954): 在 com.example.androidserverapp.Utilities.initialize(Utilities.java:8) 07-12 11:35:04.473:

关于java - Android/Java - 来自类的通信返回 java.lang.nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17612076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com