我目前正在学习Android编程。我知道这个错误出现在这个论坛有数百万次了,但请帮助我找到我错过的内容。
问题是,当我在Android模拟器上运行我的应用程序时,应用程序崩溃并显示弹出窗口:"不幸的是,Data Mahasiswa已停止。"
这是整个logcat堆栈跟踪:
06-11 17:50:52.203 1700-1700/com.example.user.tugasss I/art: Late-enabling -Xcheck:jni
06-11 17:50:52.203 1700-1700/com.example.user.tugasss W/art: Unexpected CPU variant for X86 using defaults: x86
06-11 17:50:52.783 1700-1700/com.example.user.tugasss W/System: ClassLoader referenced unknown path: /data/app/com.example.user.tugasss-1/lib/x86
06-11 17:50:53.324 1700-1700/com.example.user.tugasss D/AndroidRuntime: Shutting down VM
06-11 17:50:53.325 1700-1700/com.example.user.tugasss E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.tugasss, PID: 1700
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.tugasss/com.example.user.tugasss.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(int)' on a null object reference
at com.example.user.tugasss.MainActivity.setFragmentTitle(MainActivity.java:111)
at com.example.user.tugasss.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
DataBaseHelper.java
package com.example.user.tugasss;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mahasiswadb";
private static final int DATABASE_VERSION = 1;
public static final String TABEL_MAHASISWA = "mahasiswa";
public static final String TABEL_ANGKATAN = "angkatan";
public static final String ID = "id";
public static final String NAMA = "nama";
public static final String TANGGAL_LAHIR = "tgllahir";
public static final String JURUSAN = "jurusan";
public static final String ANGKATAN = "agt_id";
public static final String CREATE_TABEL_MAHASISWA = "CREATE TABLE IF NOT EXISTS "
+ TABEL_MAHASISWA + "(" + ID + " INTEGER PRIMARY KEY, "
+ NAMA + " TEXT, " + JURUSAN + " TEXT, "
+ TANGGAL_LAHIR + " DATE, " + ANGKATAN + "INT, " + "FOREIGN KEY(" + ANGKATAN + ") REFERENCES "
+ TABEL_ANGKATAN + " (id) " + ");";
public static final String CREATE_TABEL_ANGKATAN = "CREATE TABLE IF NOT EXISTS "
+ TABEL_ANGKATAN + "(" + ID + " INTEGER PRIMARY KEY,"
+ NAMA + " TEXT"+");";
private static DataBaseHelper instance;
public static synchronized DataBaseHelper getHelper(Context context) {
if (instance == null)
instance = new DataBaseHelper(context);
return instance;
}
private DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onOpen(SQLiteDatabase db)
{
super.onOpen(db);
if (!db.isReadOnly()){
//enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABEL_ANGKATAN);
db.execSQL(CREATE_TABEL_MAHASISWA);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MainActivity.java
package com.example.user.tugasss;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import com.example.user.tugasss.CustomDialogFragment.DialogFragmentListener;
public class MainActivity extends FragmentActivity implements DialogFragmentListener {
private Fragment contentFragment;
private ListFragment mahasiswaListFragment;
private AddFragment mahasiswaAddFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
AngkatanDAO agtDAO = new AngkatanDAO(this);
//angkatans
if(agtDAO.getAngkatans().size() <= 0)
agtDAO.loadAngkatans();
/*
* This is called when orientation is changed.
*/
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("content")) {
String content = savedInstanceState.getString("content");
if (content.equals(AddFragment.ARG_ITEM_ID)) {
if (fragmentManager
.findFragmentByTag(AddFragment.ARG_ITEM_ID) != null) {
setFragmentTitle(R.string.add_mhs);
contentFragment = fragmentManager
.findFragmentByTag(AddFragment.ARG_ITEM_ID);
}
}
}
if (fragmentManager.findFragmentByTag(ListFragment.ARG_ITEM_ID) != null) {
mahasiswaListFragment = (ListFragment) fragmentManager
.findFragmentByTag(ListFragment.ARG_ITEM_ID);
contentFragment = mahasiswaListFragment;
}
} else {
mahasiswaListFragment = new ListFragment();
setFragmentTitle(R.string.app_name);
switchContent(mahasiswaListFragment, ListFragment.ARG_ITEM_ID);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
if (contentFragment instanceof AddFragment) {
outState.putString("content", AddFragment.ARG_ITEM_ID);
} else {
outState.putString("content", ListFragment.ARG_ITEM_ID);
}
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
setFragmentTitle(R.string.add_mhs);
mahasiswaAddFragment = new AddFragment();
switchContent(mahasiswaAddFragment, AddFragment.ARG_ITEM_ID);
return true;
}
return super.onOptionsItemSelected(item);
}
/*
* We consider EmpListFragment as the home fragment and it is not added to
* the back stack.
*/
public void switchContent(Fragment fragment, String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
while (fragmentManager.popBackStackImmediate())
;
if (fragment != null) {
FragmentTransaction transaction = fragmentManager
.beginTransaction();
transaction.replace(R.id.content_frame, fragment, tag);
if (!(fragment instanceof ListFragment)) {
transaction.addToBackStack(tag);
}
transaction.commit();
contentFragment = fragment;
}
}
protected void setFragmentTitle(int resourseId) {
setTitle(resourseId);
getActionBar().setTitle(resourseId);
}
/*
* We call super.onBackPressed(); when the stack entry count is > 0. if it
* is instanceof EmpListFragment or if the stack entry count is == 0, then
* we prompt the user whether to quit the app or not by displaying dialog.
* In other words, from EmpListFragment on back press it quits the app.
*/
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
super.onBackPressed();
} else if (contentFragment instanceof ListFragment
|| fm.getBackStackEntryCount() == 0) {
//tampilkan dialog ketika keluar
onShowQuitDialog();
}
}
public void onShowQuitDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Anda Ingin Keluar?");
builder.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
builder.setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show();
}
@Override
public void onFinishDialog() {
if (mahasiswaListFragment != null) {
mahasiswaListFragment.updateView();
}
}
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
这里使用这些字符串来创建表格:
我不确定你最后一个参数TABEL_ANGKATAN你想要什么,所以目前我把它作为文本,如果你想要其他相应的改变
public static final String CREATE_TABEL_MAHASISWA = "CREATE TABLE IF NOT EXISTS "
+ TABEL_MAHASISWA + "(" + ID + " INTEGER PRIMARY KEY, "
+ NAMA + " TEXT, " + JURUSAN + " TEXT, "
+ TANGGAL_LAHIR + " DATE" + ")"
+ TABEL_ANGKATAN + " TEXT"+");";
//corrected the second query as it was incomplete
public static final String CREATE_TABEL_ANGKATAN = "CREATE TABLE IF NOT EXISTS "
+ TABEL_ANGKATAN + "(" + ID + " INTEGER PRIMARY KEY,"
+ NAMA + " TEXT"+");";
使用这些将删除您获得的当前错误