I have 2 views, MainActivity
and UnitActivity
. In main activity there are some buttons and when user clicks on them I want to bring uo UnitActivity
.
In UnitActivity
layout file there are some views like TextView. For example, if the user clicks on a button called btn_marine
I want to fill the layout with information about marine (a unit in starcraft game, if you know :P).
So my idea is to when user click on button of each unit I fill the text and images of the UnitActivity
layout with the information I have about that unit.
At the moment I am trying to hard code this I mean before starting the intent, and I have events for buttons in OnCreate
method of MainActivity
:
Button btn_marine = (Button) findViewById(R.id.btn_unit_marine);
btn_marine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, UnitActivity.class);
TextView unitNameView = (TextView) findViewById(R.id.textViewUnitName);
unitNameView.setText("Marine!"); //this is line 28 with error
startActivity(intent);
}
});
But as soon as I click the button application exits with this error in logcat
:
07-29 14:56:03.906 12696-12696/com.eetutorials.terranmate E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.eetutorials.terranmate.MainActivity$1.onClick(MainActivity.java:28)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
So my question is what is the problem in my code, and also, what you think is the best way to do such thing? I tried to create a Unit
class that holds information about each unit but I dont know how can I fill the Intent from that class.
I mean how can I pass that class to UnitActivity
so the views automatically gets populated?
This is for example Unit
class I wanted to make:
public class Unit
{
private String Name;
private String HitPoint;
private String Armor;
private DamageModifiers DamageModifier;
private String BaseDamage;
private String Description;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getHitPoint() {
return HitPoint;
}
public void setHitPoint(String hitPoint) {
HitPoint = hitPoint;
}
public String getArmor() {
return Armor;
}
public void setArmor(String armor) {
Armor = armor;
}
public DamageModifiers getDamageModifier() {
return DamageModifier;
}
public void setDamageModifier(DamageModifiers damageModifier) {
DamageModifier = damageModifier;
}
public String getBaseDamage() {
return BaseDamage;
}
public void setBaseDamage(String baseDamage) {
BaseDamage = baseDamage;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}