我只想使用log cat检查数据。 因此,当我点击一个按钮时,它将转到另一个活动,它将记录日志cat中的数据。 然而, 它给了我一股力量。
我正在尝试进行简单的检查,没有别的。
有人可以引导我吗? 感谢
logcat的:
01-27 21:38:23.366: E/AndroidRuntime(26992): FATAL EXCEPTION: main
01-27 21:38:23.366: E/AndroidRuntime(26992): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fuellogproject/com.example.fuellogproject.summary}: java.lang.NullPointerException
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.app.ActivityThread.access$700(ActivityThread.java:140)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.os.Looper.loop(Looper.java:137)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.app.ActivityThread.main(ActivityThread.java:4921)
01-27 21:38:23.366: E/AndroidRuntime(26992): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 21:38:23.366: E/AndroidRuntime(26992): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 21:38:23.366: E/AndroidRuntime(26992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-27 21:38:23.366: E/AndroidRuntime(26992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-27 21:38:23.366: E/AndroidRuntime(26992): at dalvik.system.NativeStart.main(Native Method)
01-27 21:38:23.366: E/AndroidRuntime(26992): Caused by: java.lang.NullPointerException
01-27 21:38:23.366: E/AndroidRuntime(26992): at com.example.fuellogproject.summary.onCreate(summary.java:39)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.app.Activity.performCreate(Activity.java:5188)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-27 21:38:23.366: E/AndroidRuntime(26992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
01-27 21:38:23.366: E/AndroidRuntime(26992): ... 11 more
这是我的代码
public class summary extends Activity {
TextView month;
TextView avgPrice;
TextView exFuel;
TextView avgFC;
Button doneButton;
Button exitButton;
private String bundleID;
private String bundleDate;
private String bundlePrice;
private String bundlePump;
private String bundleCost;
private String bundleOdometer;
private String bundlePreOdometer;
private String bundleFcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.summary);
month = (TextView)findViewById(R.id.month);
avgPrice = (TextView)findViewById(R.id.showavfPriceTV);
exFuel = (TextView)findViewById(R.id.showexFuelTV);
avgFC = (TextView)findViewById(R.id.showavgFCTV);
doneButton = (Button)findViewById(R.id.doneBTN);
exitButton = (Button)findViewById(R.id.exitBTN);
Bundle takeBundledData = getIntent().getExtras();
// First we need to get the bundle data that pass from the updateAnddelete
bundleID = takeBundledData.getString("clickedID");
bundleDate = takeBundledData.getString("clickedDate");
bundlePrice = takeBundledData.getString("clickedPrice");
bundlePump = takeBundledData.getString("clickedPump");
bundleCost = takeBundledData.getString("clickedCost");
bundleOdometer = takeBundledData.getString("clickedOdometer");
bundlePreOdometer = takeBundledData.getString("clickedpreOdometer");
bundleFcon = takeBundledData.getString("clickedFCon");
Log.d("Bundle","bundle : "+ takeBundledData);
}
}
update.java
private ArrayList<fuelLogPojo> pojoArrayList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_all);
// Initialize UI components
dateListView = (ListView) findViewById(R.id.allListView);
dateListView.setOnItemClickListener(this);
addButton = (Button) findViewById(R.id.addBTN);
addButton.setOnClickListener(this);
summaryButton = (Button) findViewById(R.id.summaryBTN);
addButton.setOnClickListener(this);
/*
* pojoArrayList = new ArrayList<fuelLogPojo>();
*
* // For the third argument, we need a List that contains Strings. //We
* decided to display undergraduates names on the ListView. //Therefore
* we need to create List that contains undergraduates names
* LogListAdapter = new ArrayAdapter<String>(this,
* android.R.layout.simple_list_item_1, populateList());
*
* dateListView.setAdapter(LogListAdapter);
*/
}
public void onClick(View v) {
Intent mainAct = new Intent(this, MainActivity.class);
startActivity(mainAct);
}
// To create a List that contains undergraduate names, we have to read the
// SQLite database
// We are going to do it in the separate method
public List<String> populateList() {
// We have to return a List which contains only String values. Lets
// create a List first
List<String> dateList = new ArrayList<String>();
// First we need to make contact with the database we have created using
// the DbHelper class
AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);
// Then we need to get a readable database
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
// We need a a guy to read the database query. Cursor interface will do
// it for us
// (String table, String[] columns, String selection, String[]
// selectionArgs, String groupBy, String having, String orderBy)
Cursor cursor = sqliteDatabase.query(
AndroidOpenDbHelper.TABLE_NAME_LOG, null, null, null, null,
null, null);
// Above given query, read all the columns and fields of the table
startManagingCursor(cursor);
// Cursor object read all the fields. So we make sure to check it will
// not miss any by looping through a while loop
while (cursor.moveToNext()) {
// In one loop, cursor read one undergraduate all details
// Assume, we also need to see all the details of each and every
// undergraduate
// What we have to do is in each loop, read all the values, pass
// them to the POJO class
// and create a ArrayList of undergraduates
String id = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.KEY_ROWID));
Log.d("ViewAll","id : "+id);
String date = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.KEY_DATE));
String price = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.KEY_PRICE));
String pump = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.KEY_FUEL));
String cost = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.KEY_COST));
String odm = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.KEY_ODM));
String preodm = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.KEY_PREODM));
String fcon = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.KEY_CON));
// Finish reading one raw, now we have to pass them to the POJO
fuelLogPojo logPojoClass = new fuelLogPojo();
logPojoClass.setid(id);
logPojoClass.setdate(date);
logPojoClass.setprice(price);
logPojoClass.setpump(pump);
logPojoClass.setcost(cost);
logPojoClass.setodometer(odm);
logPojoClass.setpreodometer(preodm);
logPojoClass.setfcon(fcon);
// Lets pass that POJO to our ArrayList which contains
// undergraduates as type
pojoArrayList.add(logPojoClass);
// But we need a List of String to display in the ListView also.
// That is why we create "uGraduateNamesList"
dateList.add(date);
}
// If you don't close the database, you will get an error
sqliteDatabase.close();
return dateList;
}
@Override
public void startManagingCursor(Cursor c) {
// TODO Auto-generated method stub
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
super.startManagingCursor(c);
}
}
// If you don't write the following code, you wont be able to see what you
// have just insert to the database
@Override
protected void onResume() {
super.onResume();
pojoArrayList = new ArrayList<fuelLogPojo>();
LogListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, populateList());
dateListView.setAdapter(LogListAdapter);
}
/*
* protected void onStart() { super.onStart(); // pojoArrayList = new
* ArrayList<fuelLogPojo>(); LogListAdapter = new ArrayAdapter<String>(this,
* android.R.layout.simple_list_item_1, populateList());
* dateListView.setAdapter(LogListAdapter); }
*/
// On ListView you just see the name of the undergraduate, not any other
// details
// Here we provide the solution to that. When the user click on a list item,
// he will redirect to a page where
// he can see all the details of the undergraduate
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Clicked on :" + arg2,
Toast.LENGTH_SHORT).show();
// We want to redirect to another Activity when the user click an item
// on the ListView
Intent changesAct = new Intent(this, updateAnddelete.class);
// We have to identify what object, does the user clicked, because we
// are going to pass only clicked object details to the next activity
// What we are going to do is, get the ID of the clicked item and get
// the values from the ArrayList which has
// same array id.
fuelLogPojo clickedObject = pojoArrayList.get(arg2);
// We have to bundle the data, which we want to pass to the other
// activity from this activity
Bundle dataBundle = new Bundle();
dataBundle.putString("clickedID", clickedObject.getid());
dataBundle.putString("clickedDate", clickedObject.getdate());
dataBundle.putString("clickedPrice", clickedObject.getprice());
dataBundle.putString("clickedPump", clickedObject.getpump());
dataBundle.putString("clickedCost", clickedObject.getcost());
dataBundle.putString("clickedOdometer", clickedObject.getodometer());
dataBundle.putString("clickedpreOdometer",
clickedObject.getpreodometer());
dataBundle.putString("clickedFCon", clickedObject.getfcon());
Log.i("FuelLog", "dataBundle " + dataBundle);
// Attach the bundled data to the intent
changesAct.putExtras(dataBundle);
// Start the Activity
startActivity(changesAct);
}
public void summaryClick(View v) {
Intent summary = new Intent(this, summary.class);
startActivity(summary);
}
}
答案 0 :(得分:2)
看起来你的takedBundledData
Bundle是空的。
您可以通过移动声明来确认:
Log.d("Bundle","bundle : "+ takeBundledData);
之前
bundleID = takeBundledData.getString("clickedID");
检查您是否实际从调用活动中发送了包含Intent的包。
<强>更新强>
当您启动摘要活动时,您不会发送任何具有意图的数据:
public void summaryClick(View v) {
Intent summary = new Intent(this, summary.class);
//NO DATA
startActivity(summary);
}
但是当你开始更新活动时,你正在传递数据:
Bundle dataBundle = new Bundle();
//DATA ADDED HERE
dataBundle.putString("clickedID", clickedObject.getid());
dataBundle.putString("clickedDate", clickedObject.getdate());
dataBundle.putString("clickedPrice", clickedObject.getprice());
dataBundle.putString("clickedPump", clickedObject.getpump());
dataBundle.putString("clickedCost", clickedObject.getcost());
dataBundle.putString("clickedOdometer", clickedObject.getodometer());
dataBundle.putString("clickedpreOdometer",
clickedObject.getpreodometer());
dataBundle.putString("clickedFCon", clickedObject.getfcon());
Log.i("FuelLog", "dataBundle " + dataBundle);
// Attach the bundled data to the intent
changesAct.putExtras(dataBundle);
// Start the Activity
startActivity(changesAct);
因此,将数据传递给摘要活动的方式与将其传递给Update活动的方式相同。