尝试添加启动画面时出错 - android app

时间:2015-04-01 04:59:52

标签: java android xml android-layout

我是一个机器人新手

我正在关注这个教程Android splash screen howto,它有点过时,但无论如何。

我只是将新的布局文件splash.xml添加到res / layout中,然后将图像添加到drawable / mdpi中,然后将主图像更改为MainActivity.java,即:

setContentView(R.layout.activity_main);

setContentView(R.layout.splash);

activity_main作为默认布局的名称,activity_main.xml和splash名为splash.xml的其他布局的名称。

但是应用程序停止了,说应用程序已经停止了,我甚至无法访问它。

我不知道我在这里缺少什么,这是MainActivity.java上有趣的代码:

 protected void onCreate(Bundle savedInstanceState) 
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);



    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(
                         WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.splash);

    relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
    relativeLayout.setDrawingCacheEnabled(true);
    cameraSurfaceView = (SurfaceView)
                                       findViewById(R.id.surfaceView1);
  //  cameraSurfaceView.setLayoutParams(new FrameLayout.LayoutParams(640, 480));
    cameraSurfaceHolder = cameraSurfaceView.getHolder();
    cameraSurfaceHolder.addCallback(this);
//    cameraSurfaceHolder.setType(SurfaceHolder.
  //                                               SURFACE_TYPE_PUSH_BUFFERS);




    btnCapture = (Button)findViewById(R.id.button1);
    btnCapture.setOnClickListener(new OnClickListener() 
    {   
      @Override
      public void onClick(View v) 
      {
         // TODO Auto-generated method stub
         camera.takePicture(cameraShutterCallback, 
                                       cameraPictureCallbackRaw,
                                       cameraPictureCallbackJpeg);
      }
    });
  } 

我的activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/containerImg"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <SurfaceView
            android:id="@+id/surfaceView1"
            android:layout_width="1276px"
            android:layout_height="745px"
            android:layout_centerInParent="true" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/surfaceView1"
            android:layout_alignLeft="@+id/surfaceView1"
            android:layout_marginLeft="20px"
            android:src="@drawable/mark3" />

</RelativeLayout>

<Button
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="right|center_vertical"
   android:background="@drawable/camera" />

</FrameLayout>

我的splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ImageView
    android:src="@drawable/splash"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY"/>
</LinearLayout>

有什么想法吗?

也许我应该使用相同的activity_main.xml用于此目的,而不是非常确定......

提前致谢!

修改

logcat的:

3774-3774/com.kkoci.photo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.kkoci.photo/com.kkoci.photo.MainActivity}:     java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.access$700(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:175)
        at android.app.ActivityThread.main(ActivityThread.java:5279)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
        at com.kkoci.photo.MainActivity.onCreate(MainActivity.java:65)
        at android.app.Activity.performCreate(Activity.java:5283)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);

这些是activity_main xml文件中显示的视图..当您更改布局文件时,为什么要访问这些视图?