简体   繁体   English

测试时启动屏幕后应用程序强制关闭

[英]App force closes after splash screen on testing

package com.ave;

import com.ave.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;



public class Menu extends Activity{
ImageButton select;    
int isClicked = 0;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                select = (ImageButton)findViewById(R.id.select);
        select.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                     if (isClicked == 0){
                        select.setImageResource(R.drawable.select_pressed);
                        isClicked = 1;
                     }
                     else{
                        select.setImageResource(R.drawable.select);
                        isClicked = 0;
                     }
                }});
        }

        ImageButton audio;    
        int isClicked1 = 0;{

        audio = (ImageButton)findViewById(R.id.audio);
        audio.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                     if (isClicked1 == 0){
                        audio.setImageResource(R.drawable.audio_pressed);
                        isClicked1 = 1;
                     }
                     else{
                        audio.setImageResource(R.drawable.audio);
                        isClicked1 = 0;
                     }
                 }
           });
         }
      }

============LogCat================= ============LogCat==================

E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ave/com.ave.Menu}: java.lang.NullPointerException
E AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1618)
E AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
E AndroidRuntime:     at android.app.ActivityThread.access$1500(ActivityThread.java:124)
E AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
E AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
E AndroidRuntime:     at android.os.Looper.loop(Looper.java:123)
E AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:3806)
E AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
E AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:507)
E AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
E AndroidRuntime: Caused by: java.lang.NullPointerException
E AndroidRuntime:     at android.app.Activity.findViewById(Activity.java:1693)
E AndroidRuntime:     at com.ave.Menu.(Menu.java:41)
E AndroidRuntime:     at java.lang.Class.newInstanceImpl(Native Method)
E AndroidRuntime:     at java.lang.Class.newInstance(Class.java:1409)
E AndroidRuntime:     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610)
E AndroidRuntime: ... 11 more

I have no errors within xml or class and everything is referenced properly but when i try to test the app through AVD or my phone it force stops after the splash screen.我在 xml 或 class 中没有错误,并且所有内容都已正确引用,但是当我尝试通过 AVD 或我的手机测试应用程序时,它会在启动画面后强制停止。 And produces what you see in the LogCat.并生成您在 LogCat 中看到的内容。 Again no errors in the xml or classes and everything is referenced. xml 或类中再次没有错误,并且所有内容都被引用。 So what am i doing wrong?那我做错了什么? Or what needs to be added.或者需要添加什么。 It all stopped working after adding the second imageButton method, but if i remove it.添加第二个 imageButton 方法后,这一切都停止了工作,但是如果我将其删除。 It works fine.它工作正常。

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/droid_background">
    <TextView 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
    android:text="@string/hello"
    android:textColor="#FFFFFF"
    android:textStyle="bold"/>
    <ImageButton 
android:src="@drawable/select"
android:id="@+id/select"
android:layout_height="30dp"
android:layout_width="120dp"
android:background="@null"
    android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>
    <ImageButton
android:src="@drawable/audio"
android:id="@+id/audio"
android:layout_height="30dp"
android:layout_width="120dp"
android:background="@null"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
    </RelativeLayout>'

Did you tried to run "Clean Up" to the project?您是否尝试对项目运行“清理”? Sometimes XML layouts needs to be regenerated in order app to work as expected.有时需要重新生成 XML 布局才能使应用程序按预期工作。

Did you add your Activity to the AndroidManifest.xml file?您是否将 Activity 添加到 AndroidManifest.xml 文件?

You have two ImageButtons with the id audio.您有两个 ID 为音频的 ImageButton。 Try renaming one.尝试重命名一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM