应用程序不断崩溃并提供nullPointerException

时间:2014-10-13 06:32:58

标签: 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: 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

这是MainActivity:

public class MainActivity extends FragmentActivity {

private int totalMoney = 0;
private int moneyPerSec = 1;
private int moneyPerClick = 1;
Handler handleCounter = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     

     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);

    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++;
            }
        }
    });
}

Runnable updateCounter = new Runnable(){
    public void run(){
        totalMoney += moneyPerSec;
        counter.setText(totalMoney);
        handleCounter.postDelayed(this, 1000);
    }
};



private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos){
        switch(pos){
        case 0: return StandFragment.newInstance("STAND");
        case 1: return CartFragment.newInstance("CART");
        case 2: return ThirdFragment.newInstance("THIRD");
        case 3: return ThirdFragment.newInstance("FOURTH");
        case 4: return ThirdFragment.newInstance("FIFTH");
        default: return ThirdFragment.newInstance("Default");
        }
    }

    @Override
    public int getCount(){
        return 5;
    }
}
}

更改了我初始化的位置,仍然抛出相同的错误。这是更新后的代码:

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++;
            }
        }
    });
}

更新了LogCat:

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进行初始化。

这就是原因。在将代码移动到片段之后,我在findViewById之前添加了v。并且它有效。此外,我还记得我以前的一个错误。它不会让我为文本分配变量,我必须做.setText(“”+变量)。

5 个答案:

答案 0 :(得分:1)

你应该移动这个

 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(...)

之后的setContentView(R.layout.activity_main);

修正:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);    

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);

..........

答案 1 :(得分:0)

此控件的初始化应该在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);

答案 2 :(得分:0)

您正在错误的位置初始化您的按钮在设置onClickListener之前和onCreate()

之后在setContentView()方法中初始化它们

setContentView之前一样,您的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);
}

答案 3 :(得分:0)

您应该在setContentView(<layout_id>);之后初始化您的小部件 由于您已将窗口小部件初始化为类成员,因此它未附加到任何视图,并且它们被初始化为null。

您应该在使用前初始化您的小部件

onCrate(...);

后,您忘记在buttonMoney buttonMoneyPerSecsetContentView(<layout_id>);中初始化按钮
buttonMoney= (Button)findViewById(R.id.<your_btn_id>);

buttonMoneyPerSec= (Button)findViewById(R.id.<your_btn_id>);

然后在按钮上设置onClickListener

NPE的原因

null.setOnClickListener(...);

答案 4 :(得分:0)

你的代码应该这样,

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++;
        }
    }
});
}