Java 应用程序不断崩溃并发出nullPointerException
每次我运行我的应用程序时,它都会立即崩溃并抛出Java 应用程序不断崩溃并发出nullPointerException,java,android,Java,Android,每次我运行我的应用程序时,它都会立即崩溃并抛出NullPointerException。我不知道在哪里,为什么 LogCat: 10-13 02:23:05.028: E/AndroidRuntime(1610): FATAL EXCEPTION: main 10-13 02:23:05.028: E/AndroidRuntime(1610): Process: com.association.icecreammafiatesting3, PID: 1610 10-13 02:23:05.028
NullPointerException
。我不知道在哪里,为什么
LogCat:
10-13 02:23:05.028: E/AndroidRuntime(1610): FATAL EXCEPTION: main
10-13 02:23:05.028: E/AndroidRuntime(1610): Process: com.association.icecreammafiatesting3, PID: 1610
10-13 02:23:05.028: E/AndroidRuntime(1610): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.association.icecreammafiatesting3/com.association.icecreammafiatesting3.MainActivity}: java.lang.NullPointerException
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.os.Looper.loop(Looper.java:136)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 02:23:05.028: E/AndroidRuntime(1610): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 02:23:05.028: E/AndroidRuntime(1610): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 02:23:05.028: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 02:23:05.028: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 02:23:05.028: E/AndroidRuntime(1610): at dalvik.system.NativeStart.main(Native Method)
10-13 02:23:05.028: E/AndroidRuntime(1610): Caused by: java.lang.NullPointerException
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.Activity.findViewById(Activity.java:1884)
10-13 02:23:05.028: E/AndroidRuntime(1610): at com.association.icecreammafiatesting3.MainActivity.<init>(MainActivity.java:19)
10-13 02:23:05.028: E/AndroidRuntime(1610): at java.lang.Class.newInstanceImpl(Native Method)
10-13 02:23:05.028: E/AndroidRuntime(1610): at java.lang.Class.newInstance(Class.java:1208)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-13 02:23:05.028: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
10-13 02:23:05.028: E/AndroidRuntime(1610): ... 11 more
更改了初始化的位置,仍然抛出相同的错误。以下是更新的代码:
public class MainActivity extends FragmentActivity {
private int totalMoney = 0;
private int moneyPerSec = 1;
private int moneyPerClick = 1;
TextView counter;
TextView perSecCounter;
Handler handleCounter = new Handler();
Button buttonMoney;
Button buttonMoneyPerSec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonMoney = (Button)findViewById(R.id.buttonMoney);
buttonMoneyPerSec = (Button)findViewById(R.id.buttonMoneyPerSec);
counter = (TextView)findViewById(R.id.tvMoney);
perSecCounter = (TextView)findViewById(R.id.tvMonPerSec);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
handleCounter.post(updateCounter);
buttonMoney.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
totalMoney += moneyPerClick;
counter.setText(totalMoney);
}
});
buttonMoneyPerSec.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (totalMoney>=10){
totalMoney-=10;
counter.setText(totalMoney);
moneyPerSec++;
}
}
});
}
更新日志:
10-13 12:21:46.236: E/AndroidRuntime(4323): FATAL EXCEPTION: main
10-13 12:21:46.236: E/AndroidRuntime(4323): Process: com.association.icecreammafiatesting3, PID: 4323
10-13 12:21:46.236: E/AndroidRuntime(4323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.association.icecreammafiatesting3/com.association.icecreammafiatesting3.MainActivity}: java.lang.NullPointerException
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.os.Looper.loop(Looper.java:136)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 12:21:46.236: E/AndroidRuntime(4323): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 12:21:46.236: E/AndroidRuntime(4323): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 12:21:46.236: E/AndroidRuntime(4323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 12:21:46.236: E/AndroidRuntime(4323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 12:21:46.236: E/AndroidRuntime(4323): at dalvik.system.NativeStart.main(Native Method)
10-13 12:21:46.236: E/AndroidRuntime(4323): Caused by: java.lang.NullPointerException
10-13 12:21:46.236: E/AndroidRuntime(4323): at com.association.icecreammafiatesting3.MainActivity.onCreate(MainActivity.java:40)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.Activity.performCreate(Activity.java:5231)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-13 12:21:46.236: E/AndroidRuntime(4323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-13 12:21:46.236: E/AndroidRuntime(4323): ... 11 more
正如下面的一个答案中所提到的,这可能是因为试图在主活动中初始化片段中的对象吗?因为我使用pageViewer有多个屏幕。如果是这样,我不知道如何将代码移动到该片段中。因为它不允许我在那里使用findByID进行初始化
这就是原因。将代码移动到片段后,我添加了v。在findViewById之前,它起作用了。此外,我还记得我以前犯过的一个错误。它不允许我将变量分配给文本,我必须执行.setText(“+变量)。您应该移动这个
Button buttonMoney = (Button)findViewById(R.id.buttonMoney);
Button buttonMoneyPerSec = (Button)findViewById(R.id.buttonMoneyPerSec);
TextView counter = (TextView)findViewById(R.id.tvMoney);
TextView perSecCounter = (TextView)findViewById(R.id.tvMonPerSec);
在onCreate(…)
aftersetContentView(R.layout.activity\u main)中代码>
更正:
此控件的初始化应该在oncreate方法中。所以,在setContentView之后,将这几行放在oncreate方法中,错误就会消失
TextView counter = (TextView)findViewById(R.id.tvMoney);
TextView perSecCounter = (TextView)findViewById(R.id.tvMonPerSec);
Button buttonMoney = (Button)findViewById(R.id.buttonMoney);
Button buttonMoneyPerSec = (Button)findViewById(R.id.buttonMoneyPerSec);
您在错误的位置初始化按钮在设置onClickListener之前和设置ContentView()之后在onCreate()方法中初始化按钮
与之前一样,您的JAVA文件未与任何布局文件连接或绑定,因此您将获得NPE
TextView counter;
TextView perSecCounter;
Handler handleCounter = new Handler();
Button buttonMoney;
Button buttonMoneyPerSec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize them here
// Like
counter = (TextView) findViewById(R.id.tvMoney);
perSecCounter = (TextView) findViewById(R.id.tvMonPerSec);
buttonMoney = (Button) findViewById(R.id.buttonMoney);
buttonMoneyPerSec = (Button) findViewById(R.id.buttonMoneyPerSec);
}
您应该在setContentView()之后初始化小部件代码>
由于您已经将小部件初始化为类成员,所以它没有附加到任何视图,并且它们被初始化为null
您应该在使用之前初始化小部件
这里您忘记了初始化onCrate(…)中的按钮
buttonMoney
和buttonMoneyPerSec
在setContentView()之后代码>
你的代码应该是这样的
TextView counter,perSecCounter ;
Handler handleCounter;
Button buttonMoney,buttonMoneyPerSec ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = (TextView)findViewById(R.id.tvMoney);
perSecCounter = (TextView)findViewById(R.id.tvMonPerSec);
buttonMoney = (Button)findViewById(R.id.buttonMoney);
buttonMoneyPerSec = (Button)findViewById(R.id.buttonMoneyPerSec);
handleCounter = new Handler();
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
handleCounter.post(updateCounter);
buttonMoney.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
totalMoney += moneyPerClick;
counter.setText(totalMoney);
}
});
buttonMoneyPerSec.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (totalMoney >= 10){
totalMoney -= 10;
counter.setText(totalMoney);
moneyPerSec++;
}
}
});
}
我不是在第7行和第8行做的吗?或者我必须在其他地方初始化它们吗?OP也需要对TextView执行同样的操作。在创建视图之前,不能初始化按钮。所以在设置contentview之后,用Oncreate方法初始化它们。我更改了它,但它仍然抛出相同的错误。更新的logcat是否可能重复?它必须在另一行上显示错误。不再是19岁了,现在更新。抱歉,我需要一些sleep.OP已初始化视图,但位于不同的位置。在setContentView()之前无法初始化;和创建活动时调用的onCreate方法。答案中应该有这一行。:)我更改了初始化的位置,但它仍然抛出相同的错误。您仍然得到NPE吗?您是否将初始化代码放在使用所有这些控件的上方和setcontentview下方?共享您的activity_main.xml activity_main只是一个viewpager。按钮和其他元素位于另一个片段中。这就是问题所在吗?是的,这就是问题所在,因为activity将无法初始化其他控件,并且您将获得NPE。我将其更改为类似的内容,但它仍然抛出相同的错误。现在正在更新代码。@Raistlin遇到同样的错误,请再次检查您的日志。它肯定仍然是一个NPE,我不确定它是否完全相同。但我现在就上传上面写的内容。
TextView counter;
TextView perSecCounter;
Handler handleCounter = new Handler();
Button buttonMoney;
Button buttonMoneyPerSec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize them here
// Like
counter = (TextView) findViewById(R.id.tvMoney);
perSecCounter = (TextView) findViewById(R.id.tvMonPerSec);
buttonMoney = (Button) findViewById(R.id.buttonMoney);
buttonMoneyPerSec = (Button) findViewById(R.id.buttonMoneyPerSec);
}
buttonMoney= (Button)findViewById(R.id.<your_btn_id>);
buttonMoneyPerSec= (Button)findViewById(R.id.<your_btn_id>);
null.setOnClickListener(...);
TextView counter,perSecCounter ;
Handler handleCounter;
Button buttonMoney,buttonMoneyPerSec ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = (TextView)findViewById(R.id.tvMoney);
perSecCounter = (TextView)findViewById(R.id.tvMonPerSec);
buttonMoney = (Button)findViewById(R.id.buttonMoney);
buttonMoneyPerSec = (Button)findViewById(R.id.buttonMoneyPerSec);
handleCounter = new Handler();
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
handleCounter.post(updateCounter);
buttonMoney.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
totalMoney += moneyPerClick;
counter.setText(totalMoney);
}
});
buttonMoneyPerSec.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (totalMoney >= 10){
totalMoney -= 10;
counter.setText(totalMoney);
moneyPerSec++;
}
}
});
}