0

我创建了一个名为“PhotoManagement”的简单类,这是我的实际主要活动。之后,我创建了另一个类,它扩展了活动。在第二个类中,我创建了一个方法(现在为空),我想在另一个类中调用该方法以实用地创建布局。我现在要做的只是一个简单的调用。它应该非常简单,但我收到了一个空指针异常。所以你能帮我理解我做错了什么吗?这是主要活动:

public class PhotoManagement extends Activity{

PhotoManagementLayout photoManagementLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    photoManagementLayout.buildLayout();
}

}

这是将来应该创建布局的类:

public class PhotoManagementLayout extends Activity{

  public void buildLayout(){
    ScrollView scrollView = new ScrollView(this); //create a new scrollView
    scrollView.setBackground(getResources().getDrawable(R.drawable.background)); //give the background gradient
    scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, //set the main params about the dynamic size of the scrollView
                                                 ScrollView.LayoutParams.MATCH_PARENT));
    scrollView.setPadding(0, 20, 0, 0);
    setContentView(scrollView);
  }
}

这是我收到的错误:

09-30 09:58:27.495: E/AndroidRuntime(3102): FATAL EXCEPTION: main
09-30 09:58:27.495: E/AndroidRuntime(3102): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dilandprints2/com.example.dilandprints2.PhotoManagement}: java.lang.NullPointerException
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.os.Looper.loop(Looper.java:137)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at java.lang.reflect.Method.invoke(Method.java:511)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at dalvik.system.NativeStart.main(Native Method)
09-30 09:58:27.495: E/AndroidRuntime(3102): Caused by: java.lang.NullPointerException
09-30 09:58:27.495: E/AndroidRuntime(3102):     at com.example.dilandprints2.PhotoManagement.onCreate(PhotoManagement.java:21)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.Activity.performCreate(Activity.java:5104)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-30 09:58:27.495: E/AndroidRuntime(3102):     ... 11 more

photoManagement 类存在于清单中,而 photoManagementLayout 类不存在。

谢谢

4

4 回答 4

1

您的 photoManagementLayout 为空,尚未初始化。

此外,您的两个类都在扩展 Activity,并且您正在另一个类中使用 Activity。

活动旨在通过意图一个接一个地启动,它们不是您可以使用新关键字实例化的普通类。所以你的问题不仅仅是一个简单的 NullPointerException,你误解了 Android 工作流程。

为什么不将方法代码直接放在 PhotoManager 活动的核心中?

-----------------------------------------------

只需将您的 buildLayout 方法直接放入 PhotManagment Acticity 中,然后在 onCreate 中调用它,它应该可以工作


编辑 这是一种(或多或少)将 buildLayout 方法与活动分离的方法:

public class PhotoManagementLayout {
      public static View buildLayout(Context context){
        ScrollView scrollView = new ScrollView(context); //create a new scrollView
        scrollView.setBackground(context.getResources().getDrawable(R.drawable.background)); //give the background gradient
        scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, //set the main params about the dynamic size of the scrollView
                                                     ScrollView.LayoutParams.MATCH_PARENT));
        scrollView.setPadding(0, 20, 0, 0);
        return scrollView ; 
      }
}

在 onCreate 中:

public class PhotoManagement extends Activity{



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(PhotoManagementLayout.buildLayout(this));
}

}
于 2013-09-30T10:19:04.400 回答
1
Caused by: java.lang.NullPointerException
    at com.example.dilandprints2.PhotoManagement.onCreate(PhotoManagement.java:21)

看起来photoManagementLayout是空的photoManagementLayout.buildLayout();

于 2013-09-30T10:15:19.130 回答
1
09-30 09:58:27.495: E/AndroidRuntime(3102): Caused by: java.lang.NullPointerException
09-30 09:58:27.495: E/AndroidRuntime(3102):     at com.example.dilandprints2.PhotoManagement.onCreate(PhotoManagement.java:21)

NullPointerExceptionPhotoManagement.onCreate()

PhotoManagementLayout photoManagementLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    photoManagementLayout.buildLayout();
}

photoManagementLayout显然是null因为您从未初始化它。

PhotoManagementLayout photoManagementLayout只需声明一个变量,它是对对象photoManagementLayout的引用。PhotoManagementLayout默认情况下,引用初始化为null.

于 2013-09-30T10:15:28.947 回答
1

photoManagementLayout = new PhotoManagementLayout()

photoManagementLayout这是在执行任何调用之前需要的

于 2013-09-30T10:15:55.210 回答