应用程序无法启动

时间:2014-04-02 10:06:49

标签: java android eclipse nullpointerexception android-activity

我是Android编程新手,最近开始制作练习应用程序。

虽然代码没有错误,但在模拟器中启动时,应用程序会说“不幸的是,PocketMoney已停止”

logcat的:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate  activity  
ComponentInfo{com.example.pocketmoney/com.example.pocketmoney.PocketMoney}:   
java.lang.NullPointerException

MainActivity.java

public class PocketMoney extends Activity {


final String TAG="pocketmoney";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i(TAG, "Now in onCreate() ");
    final Button NonMeal_button=(Button)findViewById(R.id.nonmeal_button);
    final Button Meal_button=(Button)findViewById(R.id.meal_button);
    if(checkPocketValues()==false)
    {
        startSettings_Activity();
    }
    Meal_button.setOnClickListener(new OnClickListener() {
        //listener for Meal_Button 
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startMeal_Money_Activity();
        }
    });
    NonMeal_button.setOnClickListener(new OnClickListener() {
        //Listener for NonMeal_button 
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        startNonMeal_Money_Activity();  
        }
    });

}
void startSettings_Activity(){
//to start Settings_Activity
    startActivity(new Intent(getApplicationContext(),Settings_Activity.class));
}
void startMeal_Money_Activity()
{
    startActivity(new Intent(this,Meal_Money_Activity.class));
}
void startNonMeal_Money_Activity()
{
    startActivity(new Intent(this,NonMeal_Money_Activity.class));
}


boolean checkPocketValues()
{
    //to check if Pocket values are filled

    boolean SettingsFilled=true;
    Settings_Activity check=new Settings_Activity();
    if((check.preferences.getFloat(check.MealMoney,0)==0)&&check.preferences.getFloat(check.NonMealMoney, 0)==0)
    {
        SettingsFilled=false;

    }

        return SettingsFilled;
}
public void onStop()
{
    Log.i(TAG,"now onStop method is called ");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.pocket_money, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:1)

你有

Settings_Activity check=new Settings_Activity(); // wrong

看起来Settings_Activity是一个Activity类,你正在实例化那个错误的

Can i Create the object of a activity in other class?

引用Raghav

  

通过将Activity视为普通的Java类,最终得到null   上下文。因为Activity中的大多数方法都是在其Context上调用的,所以   将获得一个空指针异常,这就是你的应用程序崩溃的原因。

你确实有这个

(check.preferences.getFloat(check.MealMoney,0)

所以崩溃可能是这个Settings_Activity check=new Settings_Activity();

的bcoz

答案 1 :(得分:1)

void startSettings_Activity(){
//to start Settings_Activity
    startActivity(new Intent(getApplicationContext(),Settings_Activity.class));
}

将其替换为:

 void startSettings_Activity(){
//to start Settings_Activity
    startActivity(new Intent(this,Settings_Activity.class));
}