我创建了一个名为“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 类不存在。
谢谢