当我在ListTabActivity列表中单击一个项目(打开ItemActivity)时,程序崩溃。
日志:
12-06 22:00:45.196: D/ListTabActivity(1479): Start onItemClick() // It's in ListTabActivity
12-06 22:00:45.227: D/ListTabActivity(1479): End onItemClick() // too
12-06 22:00:48.026: D/ItemActivity(1479): Sets 2 - Case 2 Start // It's in ItemAcitivity
12-06 22:00:48.046: W/System.err(1479): java.lang.NullPointerException
12-06 22:00:48.067: W/System.err(1479): at tbf.tartarugaburguer.simulador1o1.ItemActivity.onCreate(ItemActivity.java:45)
12-06 22:00:48.097: W/System.err(1479): at android.app.Activity.performCreate(Activity.java:5104)
12-06 22:00:48.126: W/System.err(1479): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-06 22:00:48.136: W/System.err(1479): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-06 22:00:48.156: W/System.err(1479): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-06 22:00:48.176: W/System.err(1479): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-06 22:00:48.226: W/System.err(1479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-06 22:00:48.246: W/System.err(1479): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 22:00:48.266: W/System.err(1479): at android.os.Looper.loop(Looper.java:137)
12-06 22:00:48.288: W/System.err(1479): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-06 22:00:48.316: W/System.err(1479): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 22:00:48.356: W/System.err(1479): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 22:00:48.376: W/System.err(1479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-06 22:00:48.396: W/System.err(1479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-06 22:00:48.426: W/System.err(1479): at dalvik.system.NativeStart.main(Native Method)
12-06 22:46:11.416: D/AndroidRuntime(1840): Shutting down VM
12-06 22:46:11.416: W/dalvikvm(1840): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-06 22:46:11.508: E/AndroidRuntime(1840): FATAL EXCEPTION: main
12-06 22:46:11.508: E/AndroidRuntime(1840): java.lang.RuntimeException: Unable to start activity ComponentInfo{tbf.tartarugaburguer.simulador1o1/tbf.tartarugaburguer.simulador1o1.ItemActivity}: java.lang.NullPointerException
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.os.Looper.loop(Looper.java:137)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-06 22:46:11.508: E/AndroidRuntime(1840): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 22:46:11.508: E/AndroidRuntime(1840): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 22:46:11.508: E/AndroidRuntime(1840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-06 22:46:11.508: E/AndroidRuntime(1840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-06 22:46:11.508: E/AndroidRuntime(1840): at dalvik.system.NativeStart.main(Native Method)
12-06 22:46:11.508: E/AndroidRuntime(1840): Caused by: java.lang.NullPointerException
12-06 22:46:11.508: E/AndroidRuntime(1840): at tbf.tartarugaburguer.simulador1o1.ItemActivity.onCreate(ItemActivity.java:49)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.app.Activity.performCreate(Activity.java:5104)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-06 22:46:11.508: E/AndroidRuntime(1840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-06 22:46:11.508: E/AndroidRuntime(1840): ... 11 more
ListTabActivity类: (它打开,但是当它打开ItemActivity时,程序崩溃)
public class ListTabActivity extends Activity {
static int tabNum;
static int position;
static List<Item> itemList1,
itemList2,
itemList3,
itemList4;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
Bundle extras = getIntent().getExtras();
int resource = 0;
if(extras != null) {
tabNum = extras.getInt("tab");
resource = extras.getInt("resource");
}
InputStream inputStream = getResources().openRawResource(resource);
ItemParser itemParser = new ItemParser();
itemParser.parse(inputStream);
switch (tabNum) {
default:
case 1:
itemList1 = itemParser.getList(); break;
case 2:
itemList2 = itemParser.getList(); break;
case 3:
itemList3 = itemParser.getList(); break;
case 4:
itemList4 = itemParser.getList();
}
ItemArrayAdapter adapter = new ItemArrayAdapter(this, R.layout.item_list, itemParser.getList(), tabNum);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
ListTabActivity.position = position;
Log.d("ListTabActivity", "Start onItemClick()");
try {
startActivity(new Intent(ListTabActivity.this, ItemActivity.class));
} catch (Exception e) {
e.printStackTrace();
}
Log.d("ListTabActivity", "End onItemClick()");
}
});
}
}
ItemActivity:
public class ItemActivity extends Activity {
int tabNum,
itemAmount,
position;
List<Item> itemList;
TextView tvTotal;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_item);
tabNum = ListTabActivity.tabNum;
position = ListTabActivity.position;
itemAmount = MainActivity.buyItems[tabNum - 2][position];
String itemPhoto;
switch (ListTabActivity.tabNum) {
default:
case 2:
itemList = ListTabActivity.itemList1;
Log.d("ItemActivity", "Sets 2 - Case 2 Start");
try {
itemPhoto = "images/tab2/photo/" + itemList.get(position).photoId; //break;
} catch (Exception e) {
e.printStackTrace();
} finally {
itemPhoto = "images/tab2/photo/" + itemList.get(position).photoId; //break;
}
Log.d("ItemActivity", "Sets 2 - Case 2 End"); break;
case 3:
itemList = ListTabActivity.itemList2;
itemPhoto = "images/tab3/photo/" + itemList.get(position).photoId; break;
case 4:
itemList = ListTabActivity.itemList3;
itemPhoto = "images/tab4/photo/" + itemList.get(position).photoId; break;
case 5:
itemList = ListTabActivity.itemList4;
itemPhoto = "images/tab5/photo/" + itemList.get(position).photoId; break;
}
final TextView tvDescription,
tvPrice,
tvAmount;
final ImageView ivPhoto;
tvDescription=(TextView) findViewById(R.id.item_description);
tvPrice = (TextView) findViewById(R.id.item_price);
tvAmount = (TextView) findViewById(R.id.item_amount);
tvTotal = (TextView) findViewById(R.id.total);
ivPhoto = (ImageView) findViewById(R.id.item_photo);
tvDescription.setText(itemList.get(position).description);
tvPrice.setText("R$ " + itemList.get(position).price);
tvAmount.setText(itemAmount);
tvTotal.setText(0);
try {
Bitmap bitmap = BitmapFactory.decodeStream(this.getResources().getAssets().open(itemPhoto));
ivPhoto.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
ItemAcitivity
案例2中的,以下代码为null
itemList.get(position).photoId
将其打印出来并检查对象itemList
,如果是null
。
或
TabActivity
案例1 中的
itemList1 = itemParser.getList();
将是null