我正在Eclipse Juno上创建一个移动应用程序,不同类的代码中没有错误,它将在模拟器上运行。但是,当第一个屏幕加载到模拟器上并且我选择其中一个按钮时,应用程序停止运行,并且我在日志cat中收到有关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);
}
});
}
}
我的Log Cat错误是:
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'
任何帮助都将非常感谢。
答案 0 :(得分: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);
}
});
}
}