2015-03-30 284 views
-3

我在Eclipse Juno上创建了一个移动应用程序,在不同类的代码中没有错误,它将在模拟器上运行。但是,当第一个屏幕加载到模拟器上,并且我选择了其中一个按钮时,应用程序将停止运行,并且在日志猫中出现关于NullPointerException的错误,但似乎无法找到此问题的位置。有没有人有任何想法我做错了什么或导致应用程序停止?空指针异常

第一个屏幕上按钮的用途是将用户带到应用程序的不同区域,因此无需选择要去的区域,我无法在应用程序中使用太多功能。

这是我的第一个屏幕的代码: package com.example.independentretailers; import com.example.independentretailers.R; import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.content。*;

public class welcome extends Activity { 


public void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.welcome); 
addListenerOnButton(); 
addListenerOnButton1(); 
} 

//to continue from welcome screen to customer login screen 
//when customer button is clicked 
public void addListenerOnButton() { 
Button CustomerButton = (Button) findViewById(R.id.button1); 

CustomerButton.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v){ 
     Intent intent = new Intent (getApplicationContext(),customerlogin.class);  
      startActivity(intent); 

     } 
    }); 
} 

//to continue from welcome screen to retailer login screen 
//when retailer button is clicked 
public void addListenerOnButton1() { 
    Button RetailerButton = (Button) findViewById(R.id.button2); 

    RetailerButton.setOnClickListener(new OnClickListener(){  

     @Override 
     public void onClick(View v){ 
      Intent intent = new Intent (getApplicationContext(),retailerlogin.class);  
      startActivity(intent); 

     } 
    }); 

} 
} 

我的日志猫错误是:

03-30 18:56:19.716: E/ResourceType(536): Style contains key with bad entry: 0x01010479 
03-30 18:56:19.806: I/dalvikvm(536): threadid=3: reacting to signal 3 
03-30 18:56:19.876: I/dalvikvm(536): Wrote stack traces to '/data/anr/traces.txt' 
03-30 18:56:19.996: D/gralloc_goldfish(536): Emulator without GPU emulation detected. 
03-30 18:58:31.936: E/ResourceType(536): Style contains key with bad entry: 0x01010479 
03-30 18:58:31.966: D/AndroidRuntime(536): Shutting down VM 
03-30 18:58:31.966: W/dalvikvm(536): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
03-30 18:58:32.016: E/AndroidRuntime(536): FATAL EXCEPTION: main 
03-30 18:58:32.016: E/AndroidRuntime(536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.independentretailers/com.example.independentretailers.customerlogin}: java.lang.NullPointerException 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.os.Looper.loop(Looper.java:137) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.ActivityThread.main(ActivityThread.java:4424) 
03-30 18:58:32.016: E/AndroidRuntime(536): at java.lang.reflect.Method.invokeNative(Native Method) 
03-30 18:58:32.016: E/AndroidRuntime(536): at java.lang.reflect.Method.invoke(Method.java:511) 
03-30 18:58:32.016: E/AndroidRuntime(536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
03-30 18:58:32.016: E/AndroidRuntime(536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
03-30 18:58:32.016: E/AndroidRuntime(536): at dalvik.system.NativeStart.main(Native Method) 
03-30 18:58:32.016: E/AndroidRuntime(536): Caused by: java.lang.NullPointerException 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.view.ViewGroup.addViewInner(ViewGroup.java:3336) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.view.ViewGroup.addView(ViewGroup.java:3208) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.view.ViewGroup.addView(ViewGroup.java:3188) 
03-30 18:58:32.016: E/AndroidRuntime(536): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 
03-30 18:58:32.016: E/AndroidRuntime(536): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.Activity.setContentView(Activity.java:1855) 
03-30 18:58:32.016: E/AndroidRuntime(536): at com.example.independentretailers.customerlogin.onCreate(customerlogin.java:28) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.Activity.performCreate(Activity.java:4465) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
03-30 18:58:32.016: E/AndroidRuntime(536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
03-30 18:58:32.016: E/AndroidRuntime(536): ... 11 more 
03-30 18:58:32.405: I/dalvikvm(536): threadid=3: reacting to signal 3 
03-30 18:58:32.445: I/dalvikvm(536): Wrote stack traces to '/data/anr/traces.txt' 
03-30 18:58:32.625: I/dalvikvm(536): threadid=3: reacting to signal 3 
03-30 18:58:32.655: I/dalvikvm(536): Wrote stack traces to '/data/anr/traces.txt' 

任何帮助都将不胜感激。

+1

请发表您的堆栈跟踪(logcat的) – 2015-03-30 19:18:03

+0

我只是说我的堆栈跟踪到原始邮件 – Tmcc 2015-03-30 19:31:36

+0

你的错误显示“...... 11多个”。你可以扩展一下吗? – Prudhvi 2015-03-30 19:43:14

回答

0

我认为你不必将一个按钮点击监听器嵌套到另一个按钮的onClickListener中。你还声明了按钮作为实例变量,并没有初始化它们,然后你在方法中声明了具有相同名称的本地按钮。删除其中任何一个让他们工作。尝试下面的代码。

public class welcome extends Activity { 
    //Button CustomerButton; No need to declare these here. 
    //Button RetailerButton; 

    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome); 
     addListenerOnButton(); 
     addListenerOnButton1(); 
    } 

    //to continue from welcome screen to customer login screen 
    //when customer button is clicked 
    public void addListenerOnButton() { 
     Button CustomerButton = (Button) findViewById(R.id.button1); 

     CustomerButton.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v){ 
       Intent intent = new Intent (getApplicationContext(),customerlogin.class);  
       startActivity(intent); 

      } 
     }); 
    } 

    //to continue from welcome screen to retailer login screen 
    //when retailer button is clicked 
    public void addListenerOnButton1() { //This method was inside the first method earlier. 
     Button RetailerButton = (Button) findViewById(R.id.button2); 

     RetailerButton.setOnClickListener(new OnClickListener(){  

      @Override 
      public void onClick(View v){ 
       Intent intent = new Intent (getApplicationContext(),retailerlogin.class);  
       startActivity(intent); 

      } 
     }); 

    } 
}