我正在使用方法来缩短我的onCreate方法 但是,当我尝试使用我的应用程序时,我就强行关闭了!
请帮助解决这个问题
活动类(主要):
package ir.TeenStudio.ActivitiesManagement;
import java.util.ArrayList;
import java.util.HashMap;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.TextView;
public class Main extends Activity {
public static SQLiteDatabase ActivitiesListDatabase;
private SlidingMenu slidingMenu;
private ActivitiesExpandableListAdapter activitiesListViewAdapter;
private ArrayList<String> groupsList;
private HashMap<String, ArrayList<HashMap<String, String>>> childsList;
private ArrayList<HashMap<String,String>> todayChildsList;
private ArrayList<HashMap<String,String>> tomarrowChildsList;
private ArrayList<HashMap<String,String>> futureChildsList;
private ArrayList<HashMap<String,String>> oneDayChildsList;
private ExpandableListView mylist;
private TextView DayTextView;
private TextView MonthTextView;
private TextView YearTextView;
private Button mainActivity;
private Button ListActivity;
private Button Setting;
private Button Contact;
private Button Information;
private ImageButton Menu;
private ImageButton Add;
private Typeface Rezvan;
private Typeface DroidNaskh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
slidingMenu = new SlidingMenu(this);
activitiesListViewAdapter = new ActivitiesExpandableListAdapter(Main.this, groupsList, childsList);
groupsList = new ArrayList<String>();
childsList = new HashMap<String, ArrayList<HashMap<String,String>>>();
todayChildsList = new ArrayList<HashMap<String,String>>();
tomarrowChildsList = new ArrayList<HashMap<String,String>>();
futureChildsList = new ArrayList<HashMap<String,String>>();
oneDayChildsList = new ArrayList<HashMap<String,String>>();
mylist = (ExpandableListView) findViewById(R.id.activitiesListView);
DayTextView = (TextView) findViewById(R.id.day);
MonthTextView = (TextView) findViewById(R.id.month);
YearTextView = (TextView) findViewById(R.id.year);
mainActivity = (Button) findViewById(R.id.button_main_activity);
ListActivity = (Button) findViewById(R.id.button_lists_activity);
Setting = (Button) findViewById(R.id.button_setting);
Contact = (Button) findViewById(R.id.button_contact);
Information = (Button) findViewById(R.id.button_info);
Menu = (ImageButton) findViewById(R.id.menu_button);
Add = (ImageButton) findViewById(R.id.add_button);
Rezvan = Typeface.createFromAsset(getAssets(), "fonts/rezvan.ttf");
DroidNaskh = Typeface.createFromAsset(getAssets(), "fonts/droid_naskh.ttf");
// Sliding Menu
setSlidingMenu();
//TypoGraphy
setLayoutTypoGraphy();
//LayoutProgramming
setTime();
setButtonsEvent();
//Creating Database
try {
final String path = Environment.getDataDirectory() +"/data/" + getPackageName() + "/ActivitiesList.db";
ActivitiesListDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.CREATE_IF_NECESSARY);
String query ="CREATE TABLE IF NOT EXISTS Content(";
query += "Id INTEGER PRIMARY KEY AUTOINCREMENT,";
query += "Description TEXT,";
query += "AddLocation INTEGER,";
query += "Location TEXT,";
query += "AddDate INTEGER,";
query += "DateYear INTEGER,";
query += "DateMonth INTEGER,";
query += "DateDay INTEGER,";
query += "AddHour INTEGER,";
query += "Hour INTEGER,";
query += "Minutes INTEGER,";
query += "AddAlarm INTEGER,";
query += "AlarmTime INTEGER,";
query += "AddSpecial INTEGER)";
ActivitiesListDatabase.execSQL(query);
Log.d("Database", "Database Created");
} catch (Exception e) {
Log.e("Database", "Database Error");
}
//Receive Data From Database
receiveDataFromDatabase();
//Sending Data to ListView
mylist.setAdapter(activitiesListViewAdapter);
}
@Override
protected void onResume() {
super.onResume();
receiveDataFromDatabase();
activitiesListViewAdapter.notifyDataSetChanged();
}
public void setLayoutTypoGraphy() {
DayTextView.setTypeface(Rezvan);
MonthTextView.setTypeface(Rezvan);
YearTextView.setTypeface(Rezvan);
mainActivity.setTypeface(DroidNaskh);
ListActivity.setTypeface(DroidNaskh);
Setting.setTypeface(DroidNaskh);
Contact.setTypeface(DroidNaskh);
Information.setTypeface(DroidNaskh);
}
public void setTime() {
DayTextView.setText(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getDay());
YearTextView.setText(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getYear());
int solarMonth = Integer.parseInt(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getMonth());
switch (solarMonth) {
case 1:
MonthTextView.setText("فروردین");
break;
case 2:
MonthTextView.setText("اردیبهشت");
break;
case 3:
MonthTextView.setText("خرداد");
break;
case 4:
MonthTextView.setText("تیر");
break;
case 5:
MonthTextView.setText("مرداد");
break;
case 6:
MonthTextView.setText("شهریور");
break;
case 7:
MonthTextView.setText("مهر");
break;
case 8:
MonthTextView.setText("آبان");
break;
case 9:
MonthTextView.setText("آذر");
break;
case 10:
MonthTextView.setText("دی");
break;
case 11:
MonthTextView.setText("بهمن");
break;
case 12:
MonthTextView.setText("اسفند");
break;
}
}
public void receiveDataFromDatabase() {
groupsList.clear();
childsList.clear();
todayChildsList.clear();
tomarrowChildsList.clear();
futureChildsList.clear();
oneDayChildsList.clear();
groupsList.add("امروز");
groupsList.add("فردا");
groupsList.add("آینده");
groupsList.add("یه روز");
Cursor cr = ActivitiesListDatabase.rawQuery("SELECT * FROM Content", null);
if (cr.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Description", cr.getString(cr.getColumnIndex("Description")));
map.put("AddDate", cr.getString(cr.getColumnIndex("AddDate")));
map.put("AddLocation", cr.getString(cr.getColumnIndex("AddLocation")));
map.put("Location", cr.getString(cr.getColumnIndex("Location")));
map.put("DateYear", cr.getString(cr.getColumnIndex("DateYear")));
map.put("DateMonth", cr.getString(cr.getColumnIndex("DateMonth")));
map.put("DateDay", cr.getString(cr.getColumnIndex("DateDay")));
map.put("AddHour", cr.getString(cr.getColumnIndex("AddHour")));
map.put("Hour", cr.getString(cr.getColumnIndex("Hour")));
map.put("Minutes", cr.getString(cr.getColumnIndex("Minutes")));
map.put("AddAlarm", cr.getString(cr.getColumnIndex("AddAlarm")));
map.put("AlarmTime", cr.getString(cr.getColumnIndex("AlarmTime")));
map.put("AddSpecial", cr.getString(cr.getColumnIndex("AddSpecial")));
if (Integer.parseInt(cr.getString(cr.getColumnIndex("AddDate")))==1) {
if(Integer.parseInt(cr.getString(cr.getColumnIndex("DateYear")))==Integer.parseInt(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getYear())
&&Integer.parseInt(cr.getString(cr.getColumnIndex("DateMonth")))==Integer.parseInt(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getMonth())
&&Integer.parseInt(cr.getString(cr.getColumnIndex("DateDay")))==Integer.parseInt(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getDay())){
todayChildsList.add(map);
}
else if (Integer.parseInt(cr.getString(cr.getColumnIndex("DateYear")))==Integer.parseInt(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getYear())
&&Integer.parseInt(cr.getString(cr.getColumnIndex("DateMonth")))==Integer.parseInt(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getMonth())
&&Integer.parseInt(cr.getString(cr.getColumnIndex("DateDay")))==(Integer.parseInt(ir.TeenStudio.ActivitiesManagement.ShamsiCalendar.getDay()))+1) {
tomarrowChildsList.add(map);
}
else {
futureChildsList.add(map);
}
} else oneDayChildsList.add(map);
} while (cr.moveToNext());
}
childsList.put(groupsList.get(0), todayChildsList);
childsList.put(groupsList.get(1), tomarrowChildsList);
childsList.put(groupsList.get(2), futureChildsList);
childsList.put(groupsList.get(3), oneDayChildsList);
}
public void setSlidingMenu() {
slidingMenu.setMode(SlidingMenu.RIGHT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setShadowDrawable(R.drawable.menu_shadow);
slidingMenu.setShadowWidth(30);
slidingMenu.setFadeDegree(1.0f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
DisplayMetrics display = this.getResources().getDisplayMetrics();
int width = display.widthPixels;
int menu_width = width - width / 3;
if (menu_width < 100) {
menu_width = 100;
}
slidingMenu.setBehindWidth(menu_width);
slidingMenu.setMenu(R.layout.main_right);
}
public void setButtonsEvent() {
OnClickListener ButtonsEvents = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.menu_button:
slidingMenu.toggle();
break;
case R.id.add_button:
Intent popUpAddItemDialogActivity = new Intent(Main.this, AddItem.class);
startActivity(popUpAddItemDialogActivity);
break;
case R.id.button_main_activity:
//Start main activity
break;
case R.id.button_lists_activity:
//Start lists activity
break;
case R.id.button_setting:
//Start setting
break;
case R.id.button_contact:
//Start contact activity
break;
case R.id.button_info:
//Start info activity
break;
default:
break;
}
}
};
Menu.setOnClickListener(ButtonsEvents);
Add.setOnClickListener(ButtonsEvents);
}
}
这是我的清单代码:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.TeenStudio.ActivitiesManagement"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/name_application"
android:theme="@style/AppTheme" >
<activity
android:name="ir.TeenStudio.ActivitiesManagement.Splash"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ir.TeenStudio.ActivitiesManagement.Main" android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
</activity>
<activity android:theme="@android:style/Theme.Dialog" android:name="AddItem"></activity>
</application>
</manifest>
记录
08-24 22:10:53.325: E/AndroidRuntime(2144): FATAL EXCEPTION: main
08-24 22:10:53.325: E/AndroidRuntime(2144): Process: ir.TeenStudio.ActivitiesManagement, PID: 2144
08-24 22:10:53.325: E/AndroidRuntime(2144): java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.TeenStudio.ActivitiesManagement/ir.TeenStudio.ActivitiesManagement.Main}: java.lang.NullPointerException
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.os.Handler.dispatchMessage(Handler.java:102)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.os.Looper.loop(Looper.java:136)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-24 22:10:53.325: E/AndroidRuntime(2144): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 22:10:53.325: E/AndroidRuntime(2144): at java.lang.reflect.Method.invoke(Method.java:515)
08-24 22:10:53.325: E/AndroidRuntime(2144): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-24 22:10:53.325: E/AndroidRuntime(2144): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-24 22:10:53.325: E/AndroidRuntime(2144): at dalvik.system.NativeStart.main(Native Method)
08-24 22:10:53.325: E/AndroidRuntime(2144): Caused by: java.lang.NullPointerException
08-24 22:10:53.325: E/AndroidRuntime(2144): at ir.TeenStudio.ActivitiesManagement.Main.setLayoutTypoGraphy(Main.java:140)
08-24 22:10:53.325: E/AndroidRuntime(2144): at ir.TeenStudio.ActivitiesManagement.Main.onCreate(Main.java:91)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.app.Activity.performCreate(Activity.java:5231)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-24 22:10:53.325: E/AndroidRuntime(2144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-24 22:10:53.325: E/AndroidRuntime(2144): ... 11 more
任何想法?帮我解决这个问题
答案 0 :(得分:0)
您的布局中没有ID = button_main_activity
的按钮,因此当您尝试在mainActivity
方法
null
字段时,setLayoutTypoGraphy
字段为{{1}}