Reputation: 51
I am new to android programming and recently started making a practice application.
Although the code is error free the app when launched in the emulator says "Unfortunately PocketMoney has stopped"
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;
}
}
Upvotes: 1
Views: 121
Reputation: 133560
You have
Settings_Activity check=new Settings_Activity(); // wrong
Looks like Settings_Activity
is a Activity class and you are instantiating the same which is wrong
Can i Create the object of a activity in other class?
Quoting Raghav
By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.
And you do have this
(check.preferences.getFloat(check.MealMoney,0)
So the crash is probably bcoz of this Settings_Activity check=new Settings_Activity();
Upvotes: 1
Reputation: 6166
void startSettings_Activity(){
//to start Settings_Activity
startActivity(new Intent(getApplicationContext(),Settings_Activity.class));
}
replace it by :
void startSettings_Activity(){
//to start Settings_Activity
startActivity(new Intent(this,Settings_Activity.class));
}
Upvotes: 1