我正在开展一个项目,如果你在主要活动(MainActivityWithButtons)上按下Break In,它会打开一个页面供你输入你的地址,当你按下按钮时它就会发送一条短信特定的消息。我需要使用布尔值来选择该消息。如果你点击中断,它会将一个名为breakIn的布尔值设置为true。我现在需要访问breakIn布尔值和值(true或false,在本例中为true),以便使用if else语句来选择消息。我的问题是,即使我的布尔设置为真,我按下按钮发送短信,我的应用程序崩溃了!请帮助我,这里是代码段:
MainActivityWithButtons:
package com.apps.testp;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
@SuppressWarnings("deprecation")
public class MainActivityWithButtons extends ActionBarActivity {
public static boolean breakin;
public static boolean storeRobbery;
public static boolean carTheft;
public static boolean assault;
public static boolean fighting;
public static boolean shotsFired;
public static boolean changeAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_with_buttons);
Button breakIn=(Button)findViewById(R.id.houseBreakIn);
Button robbery=(Button)findViewById(R.id.storeRobbery);
Button car=(Button)findViewById(R.id.carTheft);
Button assaulting=(Button)findViewById(R.id.assault);
Button shots=(Button)findViewById(R.id.shotsFired);
Button fight=(Button)findViewById(R.id.fighting);
Button address=(Button)findViewById(R.id.setOrChangeAddress);
address.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
changeAddress=true;
startActivity(i);
}
});
breakIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
breakin=true;
startActivity(i);
}
});
robbery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
storeRobbery=true;
startActivity(i);
}
});
car.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
carTheft=true;
startActivity(i);
}
});
assaulting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
assault=true;
startActivity(i);
}
});
shots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
shotsFired=true;
startActivity(i);
}
});
fight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
fighting=true;
startActivity(i);
}
});
//Next Listener Goes Here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_with_buttons, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
LocationSelectFromAlertButtons:
package com.apps.testp;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@SuppressWarnings("deprecation")
public class LocationSelectFromAlertButtons extends MainActivityWithButtons{
DBAdapter myDB;
String addressInTV;
Button save;
TextView currentAddress;
EditText addressNew;
Button cancel;
MainActivityWithButtons mainAct;
String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_select_from_alert_buttons);
setTitle("Please Select Which Address to Use");
mainAct = new MainActivityWithButtons();
save=(Button)findViewById(R.id.saveBtn);
currentAddress=(TextView)findViewById(R.id.CurrAddress);
addressNew=(EditText)findViewById(R.id.tbNewAddress);
cancel=(Button)findViewById(R.id.cancelBtn);
final boolean breakIn=MainActivityWithButtons.breakin;
final boolean storeRobbery=MainActivityWithButtons.storeRobbery;
final boolean carTheft=MainActivityWithButtons.carTheft;
final boolean assault=MainActivityWithButtons.assault;
final boolean shotsFired=MainActivityWithButtons.shotsFired;
final boolean fighting=MainActivityWithButtons.fighting;
final boolean address=MainActivityWithButtons.changeAddress;
openDB();
Cursor cursor=myDB.getAllRows();
displayAddress(cursor);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addressInTV = LocationSelectFromAlertButtons.this.addressNew.getText().toString();
myDB.deleteAll();
long newId = myDB.insertRow(addressInTV);
Intent i = new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(i);
}
});
cancel.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}
});
currentAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(breakIn){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Break In!'/n'"+message , null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(storeRobbery){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Store Robbery!'/n'"+message, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(carTheft){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Car Theft!'/n'"+message, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(assault){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Assault!'/n'"+message, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(fighting){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Fighting!'/n'"+message, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(shotsFired){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Shots Fired!'/n'"+message, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(breakIn){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Break In!'/n'"+addressInTV , null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(storeRobbery){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Store Robbery!'/n'"+addressInTV, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(carTheft){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Car Theft!'/n'"+addressInTV, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(assault){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Assault!'/n'"+addressInTV, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(fighting){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Fighting!'/n'"+addressInTV, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(shotsFired==true){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27719276502", null, "Shots Fired!'/n'"+addressInTV, null, null);
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}else if(address){
Intent intent=new Intent(LocationSelectFromAlertButtons.this, MainActivityWithButtons.class);
startActivity(intent);
}
}
});
}
private void displayAddress(Cursor cursor) {
String message="No Currently Set Addresses";
//reset cursor to start
if(cursor.moveToFirst()){
//process data
int id=cursor.getInt(0);
String address=cursor.getString(1);
message= address;
}
currentAddress.setText(message);
cursor.close();
}
private void openDB() {
myDB = new DBAdapter(this);
myDB.open();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void closeDB() {
myDB.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.location_select_from_alert_buttons,
menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
修改 经度:
07-20 16:22:30.791 30308-30308/com.apps.testp I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
07-20 16:22:30.791 30308-30308/com.apps.testp I/SELinux﹕ SELinux: VERIFYSIG File Open Unsuccessful:
07-20 16:22:30.791 30308-30308/com.apps.testp I/SELinux﹕ Function: selinux_android_load_priority , spota verifySig or checkHash fails. priority version is VE=SEPF_GT-I9500_4.4.2_0024
07-20 16:22:30.791 30308-30308/com.apps.testp I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
07-20 16:22:30.796 30308-30308/com.apps.testp E/dalvikvm﹕ >>>>> Normal User
07-20 16:22:30.796 30308-30308/com.apps.testp E/dalvikvm﹕ >>>>> com.apps.testp [ userId:0 | appId:10127 ]
07-20 16:22:30.796 30308-30308/com.apps.testp D/dalvikvm﹕ Late-enabling CheckJNI
07-20 16:22:30.906 30308-30308/com.apps.testp W/ApplicationPackageManager﹕ getCSCPackageItemText()
07-20 16:22:30.906 30308-30308/com.apps.testp I/PersonaManager﹕ getPersonaService() name persona_policy
07-20 16:22:30.966 30308-30308/com.apps.testp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-20 16:22:30.971 30308-30308/com.apps.testp W/dalvikvm﹕ VFY: unable to resolve virtual method 411: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-20 16:22:30.971 30308-30308/com.apps.testp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-20 16:22:30.971 30308-30308/com.apps.testp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-20 16:22:30.971 30308-30308/com.apps.testp W/dalvikvm﹕ VFY: unable to resolve virtual method 433: Landroid/content/res/TypedArray;.getType (I)I
07-20 16:22:30.971 30308-30308/com.apps.testp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-20 16:22:31.151 30308-30308/com.apps.testp D/OpenGLRenderer﹕ Enabling debug mode 0
07-20 16:24:27.716 30308-30308/com.apps.testp W/ApplicationPackageManager﹕ getCSCPackageItemText()
07-20 16:24:27.716 30308-30308/com.apps.testp I/PersonaManager﹕ getPersonaService() name persona_policy
07-20 16:24:27.781 30308-30308/com.apps.testp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
07-20 16:24:27.781 30308-30308/com.apps.testp W/dalvikvm﹕ VFY: unable to resolve virtual method 374: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-20 16:24:27.781 30308-30308/com.apps.testp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-20 16:24:27.781 30308-30308/com.apps.testp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
07-20 16:24:27.781 30308-30308/com.apps.testp W/dalvikvm﹕ VFY: unable to resolve virtual method 376: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-20 16:24:27.781 30308-30308/com.apps.testp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-20 16:24:33.081 30308-30308/com.apps.testp D/AndroidRuntime﹕ Shutting down VM
07-20 16:24:33.081 30308-30308/com.apps.testp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x420a4c08)
07-20 16:24:33.086 30308-30308/com.apps.testp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.apps.testp, PID: 30308
java.lang.SecurityException: Sending SMS message: uid 10127 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:934)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:137)
at com.apps.testp.LocationSelectFromAlertButtons$3.onClick(LocationSelectFromAlertButtons.java:77)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
修改
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.apps.testp">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.apps.testp.MainActivityWithButtons"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.apps.testp.LocationSelectFromAlertButtons"
android:label="@string/title_activity_location_select_from_alert_buttons"
android:screenOrientation="portrait">
</activity>
<uses-permission android:name="android.permission.SEND_SMS" />
</application>
如果您需要更多信息,请随时发表评论并向我询问:) 非常感谢你的帮助!
答案 0 :(得分:1)
通过你的意图发送它们。
在MainActivityWithButtons中:
Intent i = new Intent(MainActivityWithButtons.this, LocationSelectFromAlertButtons.class);
i.putExtra("some_unique_string1", your_boolean_variable)
startActivity(i);
在LocationSelectFromAlertButtons中,onCreate():
boolean your_boolean_variable = getIntent().getBooleanExtra("some_unique_string1", false);
答案 1 :(得分:0)
将您的布尔值保存为SharedPreferences
的一部分。然后,您可以从应用程序的任何位置访问它。
SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
preferences.edit().putBoolean(BOOLEAN_NAME, boolean).apply();