2016-04-17 108 views
1

我創建了一個應用,需要使用imageview才能在android工作室中打開新的活動。我有它的工作,但突然它已停止工作,現在當我的圖像瀏覽被選中時崩潰。 下面是我的代碼:Android Studios應用崩潰

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

     } 
    }); 
} 

} 

有誰知道爲什麼它不是工作?其真正困惑我,謝謝

編輯:

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:支付或Order類:

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

你可以張貼crashlogs分配呢? –

+0

編輯:查看crashlogs的帖子 – IonaMc

+0

當您在PayOrder中調用'getIntExtra'時,您有'NullPointerException' –

回答

0

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

還有就是你的問題,你需要調用getIntent().getIntExtra()方法你可以保證getIntent()不會爲空。

變化,爲DinnerTable currentTable;

,然後在你的onCreate

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

+0

如果我將它改爲currentTable = = tables.get(getIntent()。getIntExtra(「Table」,-1));我收到錯誤了嗎? – IonaMc

+0

對不起,這是一個錯字,刪除第二個'=' –

+0

完美的作品。非常感謝,非常感謝! – IonaMc