IonaMc IonaMc - 7 months ago 45
Java Question

Android Studios App Crashing

I have created an app that requires a imageview to open a new activity in android studios. I had it working but suddenly it has stopped working and now crashes when my imageviews are selected.
Below is my code:

public class Options extends AppCompatActivity {

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

ImageView imgview1 = (ImageView) findViewById(R.id.imageView1);
imgview1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Options.this, PayorOrder.class);
myIntent.putExtra("Table", 1);
startActivity(myIntent);

}
});

ImageView imgview2 = (ImageView) findViewById(R.id.imageView2);
imgview2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent2 = new Intent(Options.this, PayorOrder.class);
myIntent2.putExtra("Table", 2);
startActivity(myIntent2);
}
});

ImageView imgview3 = (ImageView) findViewById(R.id.imageView3);
imgview3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent3 = new Intent(Options.this, PayorOrder.class);
myIntent3.putExtra("Table", 3);
startActivity(myIntent3);

}
});

ImageView imgview4 = (ImageView) findViewById(R.id.imageView4);
imgview4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent4 = new Intent(Options.this, PayorOrder.class);
myIntent4.putExtra("Table", 4);
startActivity(myIntent4);

}
});

ImageView imgview5 = (ImageView) findViewById(R.id.imageView5);
imgview5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent5 = new Intent(Options.this, PayorOrder.class);
myIntent5.putExtra("Table", 5);
startActivity(myIntent5);

}
});

ImageView imgview6 = (ImageView) findViewById(R.id.imageView6);
imgview6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent6 = new Intent(Options.this, PayorOrder.class);
myIntent6.putExtra("Table", 6);
startActivity(myIntent6);

}
});
}

}


Does anyone see why it isnt working? Its really confusing me, thanks

Edit:

04-17 23:16:27.940 1966-1966/com.example.ben.restaurantapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ben.restaurantapp, PID: 1966
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ben.restaurantapp/com.example.ben.restaurantapp.PayorOrder}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at com.example.ben.restaurantapp.PayorOrder.<init>(PayorOrder.java:16)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 


Edit2: Pay or Order Class:

public class PayorOrder extends AppCompatActivity {

Hashtable <Integer, DinnerTable> tables = new Hashtable<>();
DinnerTable currentTable = tables.get(getIntent().getIntExtra("Table", -1));

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

Button btn = (Button) findViewById(R.id.btnOrder);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(PayorOrder.this, Menu.class);
myIntent.putExtra("Table", (Parcelable) currentTable);
startActivity(myIntent);
}

});

if(tables.get(1) == null )
setupHashtable();
Toast t = Toast.makeText(PayorOrder.this, String.valueOf(getIntent().getIntExtra("Table", -1)), Toast.LENGTH_SHORT);
t.show();

}

private void setupHashtable() {
for (int i = 1; i < 7; i++)
{
DinnerTable dt = new DinnerTable();
dt.setTableNum(i);
tables.put(dt.getTableNum(), dt);
}
}
}

Answer

DinnerTable currentTable = tables.get(getIntent().getIntExtra("Table", -1));

There is your problem, you need to call the getIntent().getIntExtra() method where you can guarantee that the getIntent() will not be null.

change that to DinnerTable currentTable;

and then assign it during your onCreate

currentTable = tables.get(getIntent().getIntExtra("Table", -1));