我有一个NullPointerException但我无法找到该行,因为context是null。每当程序跳转到Mainactivity时,它会抛出此异常,请帮助我找到该行,我将非常感激。
package com.example.hurryapppointalert_client;
import java.util.Locale;
import java.util.StringTokenizer;
import android.R.string;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.location.Location;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.content.ActivityNotFoundException;;;
public class MainActivity extends Activity
{
String msg;
static String plat;
static String plong;
AppLocationService appLocationService;
Button bt;
static Button btstop,btmap,btask;
static TextView tv;
SmsManager sms = SmsManager.getDefault();
static Context context;
static MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sec);
context=getApplicationContext();
Data.notimgr.notify(0, Data.noti);
bt=(Button)findViewById(R.id.bt);
btmap=(Button)findViewById(R.id.btMap);
btstop=(Button)findViewById(R.id.btstop);
appLocationService = new AppLocationService(MainActivity.this);
Location location = appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
Data.ulat = location.getLatitude();
Data.ulong = location.getLongitude();
msg="Location:?"+Data.ulat + "?" + Data.ulong+ "?" + Data.pointNum;
}
else
{
Toast.makeText(context, "Turn on Wifi", Toast.LENGTH_LONG).show();
}
btmap.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(!(plat.equals(" ") && plong.equals(" ")))
{
double lat=Double.parseDouble(plat);
double longi=Double.parseDouble(plong);
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?&daddr=%f,%f", lat, longi);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException ex)
{
try
{
Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(unrestrictedIntent);
}
catch(ActivityNotFoundException innerEx)
{
Toast.makeText(context, "Please install a maps application", Toast.LENGTH_LONG).show();
}
}
}
else
Toast.makeText(context, "No latitude and longitude were found!", Toast.LENGTH_LONG).show();
}
});
btask.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
sms.sendTextMessage("+923313552256", null, "Not Recieved!", null,null);
}
});
btstop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
mp.stop();
sms.sendTextMessage("+923313552256", null,"Recieved!", null, null);
}
});
bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
try
{
sms.sendTextMessage("+923313552256", null, msg, null, null);
}
catch(IllegalArgumentException e)
{
Toast.makeText(getApplicationContext(), "Unable to retrieve location! Turn on GPS and Maps", Toast.LENGTH_LONG).show();
}
}
});
}//end of onCreate()
public static void startAlarm(String msg,Context context)
{
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mp = MediaPlayer.create(context, notification);
mp.start();
btstop.setVisibility(1);
btmap.setVisibility(1);
StringTokenizer tokens = new StringTokenizer(msg, " ");
plat = tokens.nextToken();
plong = tokens.nextToken();
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
Data.notimgr.cancel(0);
super.onDestroy();
}
}//end of class
logcat的:
09-06 19:17:26.089: E/AndroidRuntime(13108): FATAL EXCEPTION: main
09-06 19:17:26.089: E/AndroidRuntime(13108): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hurryapppointalert_client/com.example.hurryapppointalert_client.MainActivity}: java.lang.NullPointerException
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.os.Looper.loop(Looper.java:137)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.app.ActivityThread.main(ActivityThread.java:4517)
09-06 19:17:26.089: E/AndroidRuntime(13108): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 19:17:26.089: E/AndroidRuntime(13108): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 19:17:26.089: E/AndroidRuntime(13108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-06 19:17:26.089: E/AndroidRuntime(13108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-06 19:17:26.089: E/AndroidRuntime(13108): at dalvik.system.NativeStart.main(Native Method)
09-06 19:17:26.089: E/AndroidRuntime(13108): Caused by: java.lang.NullPointerException
09-06 19:17:26.089: E/AndroidRuntime(13108): at com.example.hurryapppointalert_client.MainActivity.onCreate(MainActivity.java:106)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.app.Activity.performCreate(Activity.java:4470)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
09-06 19:17:26.089: E/AndroidRuntime(13108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
09-06 19:17:26.089: E/AndroidRuntime(13108): ... 11 more
答案 0 :(得分:3)
您正在获取NPE,因为btask尚未初始化并且您正在尝试为其设置侦听器。例如 -
btask=(Button)findViewById(R.id.btAsk);