我开发了一款通过短信发送用户位置的应用。当我开始构建它时没有错误,但是当它用实际手机进行测试时它会崩溃......它无法打开。
public class MainActivity extends AppCompatActivity {
EditText phone;
Button emergency;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone= (EditText) findViewById(R.id.number);
emergency= (Button) findViewById(R.id.emergency);
emergency.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GPStracker g = new GPStracker(getApplicationContext());
Location l = g.getLocation();
if (l != null) {
double lat = l.getLatitude();
double lon = l.getLongitude();
String message = "http://maps.google.com/maps?saddr=" + lat + "," + lon;
String number = "number";
SmsManager smsManager = SmsManager.getDefault();
StringBuffer smsBody = new StringBuffer();
smsBody.append(Uri.parse(message));
android.telephony.SmsManager.getDefault().sendTextMessage(number, null, smsBody.toString(), null, null);
}
}
});
}
此处也是我的GPS追踪类
public class GPStracker implements LocationListener {
Context context;
public GPStracker(Context c){
context = c;
}
public Location getLocation(){
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled){
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10,this);
Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return l;
}else{
Toast.makeText(context,"Please enable GPS", Toast.LENGTH_LONG).show();
}
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
}
}
和我的宣言文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.research.sos.smshelpcaller">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
</application>
我已经尝试过一切,但我不知道我的代码中有什么错误,或者有些遗漏。
这里最后是LOGCAT的日志
04-03 13:55:56.472 31224-31224/com.research.sos.smshelpcaller E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.research.sos.smshelpcaller, PID: 31224
java.lang.NullPointerException: Attempt to get length of null array
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1493)
at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:1430)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:320)
at com.research.sos.smshelpcaller.MainActivity$1.onClick(MainActivity.java:37)
at android.view.View.performClick(View.java:5052)
at android.view.View$PerformClick.run(View.java:20162)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
答案 0 :(得分:1)
你的布局文件在哪里, 在super.onCreate(savedInstanceState);。
之后,将您的xml引用提供给activity例如..
Function BatchRunMails()
Dim countrow As Integer, mailAddress As String, UniqueId As Integer
Dim Source As Object
Dim FixedFilePathName As String
Dim OverwriteIfFileExist As Boolean
Dim OpenPDFAfterPublish As Boolean
Dim FileFormatstr As String
Dim Fname As Variant
Dim ws As Worksheet
countrow = 340
While Sheets(BilledSheet).Cells(countrow, "A") <> ""
mailAddress = Sheets(BilledSheet).Cells(countrow, "D")
If mailAddress <> "" Then
UniqueId = Sheets(BilledSheet).Cells(countrow, "A")
Sheets(StatementSheet).Activate
Sheets(StatementSheet).Cells(3, "O").Activate
Sheets(StatementSheet).Cells(3, "O") = UniqueId
Sheets(InvoiceSheet).Activate
Sheets(InvoiceSheet).Cells(3, "M").Activate
Sheets(InvoiceSheet).Cells(3, "M") = UniqueId
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
Macro3
Macro2
Set ws = ActiveSheet
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:="C:\Users\c730057\Documents\02. Personal Folder\Saved PDF.pdf"
End If
End If
countrow = countrow + 1
Wend
End Function
答案 1 :(得分:-1)
根据您的日志,您尝试在Null Pointer上调用方法:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.research.sos.smshelpcaller.MainActivity.onCreate(MainActivity.java:23)
这意味着,以下代码无法解析R.id.emergency
资产。它根本无法找到它并且第一行被设置为null:
emergency= (Button) findViewById(R.id.emergency);
emergency.setOnClickListener(new View.OnClickListener() {...}
您应该检查您尝试使用的按钮的名称是否正确,是否没有拼写错误,或者是否存在资源文件(例如带有视图的xml)。