由于最近添加了警报管理器,我的Android应用程序会继续强行关闭。我是报警管理员的新手,并完成了我对部分教程所做的工作。在我已经说过的代码中我已经把我的代码放在了哪里。 (不确定这是不是正确的地方?)。请注意我应该把我的代码的其他部分放在哪里,不受影响的是报警管理器。
这是我的logcat:
04-16 14:36:52.011: E/AndroidRuntime(1127): FATAL EXCEPTION: main
04-16 14:36:52.011: E/AndroidRuntime(1127): Process: com.knight, PID: 1127
04-16 14:36:52.011: E/AndroidRuntime(1127): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.knight./com.knight.MainActivity}: java.lang.NullPointerException
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.os.Handler.dispatchMessage(Handler.java:102)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.os.Looper.loop(Looper.java:136)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-16 14:36:52.011: E/AndroidRuntime(1127): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 14:36:52.011: E/AndroidRuntime(1127): at java.lang.reflect.Method.invoke(Method.java:515)
04-16 14:36:52.011: E/AndroidRuntime(1127): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-16 14:36:52.011: E/AndroidRuntime(1127): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-16 14:36:52.011: E/AndroidRuntime(1127): at dalvik.system.NativeStart.main(Native Method)
04-16 14:36:52.011: E/AndroidRuntime(1127): Caused by: java.lang.NullPointerException
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
04-16 14:36:52.011: E/AndroidRuntime(1127): at com.knight.MainActivity.<init>(MainActivity.java:44)
04-16 14:36:52.011: E/AndroidRuntime(1127): at java.lang.Class.newInstanceImpl(Native Method)
04-16 14:36:52.011: E/AndroidRuntime(1127): at java.lang.Class.newInstance(Class.java:1208)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-16 14:36:52.011: E/AndroidRuntime(1127): ... 11 more
04-16 14:36:56.491: I/Process(1127): Sending signal. PID: 1127 SIG: 9
这是我的Alarm.java:
package com.knight.;
import java.util.Random;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.widget.TextView;
import android.app.Service;
public class Alarm extends Service{
public void onStart(Intent intent, int startId) {
{
//MY CODE????
}
}
private TextView findViewById(int textview3) {
// TODO Auto-generated method stub
return null;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
这是警报设置java:
package com.knight.;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.Calendar;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import com.knight..R;
public class MainActivity extends Activity {
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Intent myIntent = new Intent(MainActivity.this, Alarm.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.set(Calendar.HOUR_OF_DAY, 0, pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
// time set up is unsuccessful in earlier versions
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}