当我单击应用程序上的按钮时,我在日志中收到上述错误。我将发布发送活动和接收活动的代码。
发送活动:
private TrackerDAO dbHelper;
private SimpleCursorAdapter dataAdapter;
String historyString = "";
long idDB;
long getlastID;
String getAD;
String Check;
public static long TripID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history_list);
try{
dbHelper = new TrackerDAO(this);
}catch(SQLException e){
e.printStackTrace();
}
dbHelper.open();
displayListView();
}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllEntries();
// The desired columns to be bound
String[] columns = new String[] {
DBConfiguration.KEY_NAME,
DBConfiguration.KEY_CITY,
DBConfiguration.KEY_REASON,
DBConfiguration.KEY_BUDGET
};
// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.TV_trip_name,
R.id.TV_city_name,
R.id.TV_trip_reason,
R.id.TV_budget,
};
//create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.history_list_item, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView_tripHistory);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
intentMethod(id);
}
private void intentMethod(long ID) {
//Bundle basket_tripID = new Bundle();
//basket_tripID.putExtra("key_ID",TripID);
TripID = ID;
Intent Openactivity = new Intent(HistoryListActivity.this, HistoryViewerActivity.class);
//Openactivity.putExtra("key_ID",id);
startActivity(Openactivity);
}
});
}
protected void onPause() {
super.onPause();
finish();
}
}
这里的接收活动:
TextView TripName, TripReason, cityName, SD, AD, budget, HE, TE, FE, EE, SE, OE, RemainBudget;
ImageButton CheckedIn;
long idDB = HistoryListActivity.TripID;
String name ;
String city;
String reason;
String startingdate;
String arrivaldate;
String tbudget;
int hexp;
int texp;
int fexp;
int eexp;
int sexp;
int oexp;
int rbudget;
TrackerDAO dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history_viewer);
TripName = (TextView) findViewById(R.id.textView_History_TripName);
cityName = (TextView) findViewById(R.id.textView_History_CityName);
TripReason = (TextView) findViewById(R.id.textView_History_TripReason);
SD = (TextView) findViewById(R.id.textView_History_SD);
AD = (TextView) findViewById(R.id.textView__History_AD);
budget = (TextView) findViewById(R.id.textView_History_Budget);
HE = (TextView) findViewById(R.id.textView_History_Hotel);
TE = (TextView) findViewById(R.id.textView_History_Transport);
FE = (TextView) findViewById(R.id.textView__History_Food);
EE = (TextView) findViewById(R.id.textView_History_Entertainment);
SE = (TextView) findViewById(R.id.textView_History_Shopping);
OE = (TextView) findViewById(R.id.textView_History_Other);
RemainBudget = (TextView) findViewById(R.id.textView_History_RemainingBudget);
CheckedIn = (ImageButton) findViewById(R.id.imageButton_CheckedIN);
displayshowHistory(idDB);
TripName.setText(name);
cityName.setText(city);
TripReason.setText(reason);
SD.setText(startingdate);
AD.setText(arrivaldate);
budget.setText(tbudget);
HE.setText(String.valueOf(hexp));
TE.setText(String.valueOf(texp));
FE.setText(String.valueOf(fexp));
EE.setText(String.valueOf(eexp));
SE.setText(String.valueOf(sexp));
OE.setText(String.valueOf(oexp));
RemainBudget.setText(String.valueOf(rbudget));
CheckedIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void displayshowHistory(long id) {
dbHelper.open();
Cursor cursor = dbHelper.getInfo(id);
if (cursor.moveToFirst()) {
//idDB = cursor.getLong(TrackerDAO.COL_ROWID);
name = cursor.getString(TrackerDAO.COL_NAME);
city = cursor.getString(TrackerDAO.COL_CITY);
reason = cursor.getString(TrackerDAO.COL_REASON);
startingdate = cursor.getString(TrackerDAO.COL_SOD);
arrivaldate = cursor.getString(TrackerDAO.COL_AD);
tbudget = cursor.getString(TrackerDAO.COL_BUDGET);
hexp = cursor.getInt(TrackerDAO.COL_HE);
texp = cursor.getInt(TrackerDAO.COL_TE);
fexp = cursor.getInt(TrackerDAO.COL_FE);
eexp = cursor.getInt(TrackerDAO.COL_EE);
sexp = cursor.getInt(TrackerDAO.COL_SE);
oexp = cursor.getInt(TrackerDAO.COL_OE);
rbudget = cursor.getInt(TrackerDAO.COL_REMAINBUDGET);
/*historyString = "Trip Name: " + tripname + "\n"
+ "City: " + city + "\n"
+ "Reason For travel: " + tripreason + "\n"
+ "Starting Date: " + sd + "\n"
+ "Date of Arrival: " + ad + "\n"
+ "Estimated Budget: " + tbudget + "\n"
+ "Hotel Rents: " + he + "\n"
+ "Tansport Fares: " + te + "\n"
+ "Food Costs: " + fe + "\n"
+ "Entertainment Spendings: " + ee + "\n"
+ "Shopping Expenditure: " + se + "\n"
+ "Other Expenses: " + oe + "\n"
+ "Remain Money: " + rbudget; */
}
dbHelper.close();
}
}
现在这里是LOGCAT:
01-28 08:23:56.612: E/AndroidRuntime(1140): FATAL EXCEPTION: main
01-28 08:23:56.612: E/AndroidRuntime(1140): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blackcat.triporganizer/com.blackcat.triporganizer.history.HistoryViewerActivity}: java.lang.NullPointerException
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.os.Looper.loop(Looper.java:137)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-28 08:23:56.612: E/AndroidRuntime(1140): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 08:23:56.612: E/AndroidRuntime(1140): at java.lang.reflect.Method.invoke(Method.java:525)
01-28 08:23:56.612: E/AndroidRuntime(1140): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-28 08:23:56.612: E/AndroidRuntime(1140): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-28 08:23:56.612: E/AndroidRuntime(1140): at dalvik.system.NativeStart.main(Native Method)
01-28 08:23:56.612: E/AndroidRuntime(1140): Caused by: java.lang.NullPointerException
01-28 08:23:56.612: E/AndroidRuntime(1140): at com.blackcat.triporganizer.history.HistoryViewerActivity.displayshowHistory(HistoryViewerActivity.java:92)
01-28 08:23:56.612: E/AndroidRuntime(1140): at com.blackcat.triporganizer.history.HistoryViewerActivity.onCreate(HistoryViewerActivity.java:63)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.app.Activity.performCreate(Activity.java:5133)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-28 08:23:56.612: E/AndroidRuntime(1140): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-28 08:23:56.612: E/AndroidRuntime(1140): ... 11 more
答案 0 :(得分:0)
我认为你在null pointer exception
方法中得到displayshowHistory()
。问题可能是该方法中未初始化的对象。所以请检查是否有任何未初始化的对象。
答案 1 :(得分:0)
在我的情况下,我刚刚将第二个活动添加到Manifest文件中,并且每件事都变得正常:
添加:<activity android:name=".MainActivity2"></activity>
注意:通过第二项活动更改
MainActivity2
。
你的最终文件将是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sadmi.darksnow.intentbundle">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"></activity>
</application>
</manifest>