java - 如何从不同的类访问 boolean 值?

标签 java android class variables

我正在开发一个项目,如果您在主要 Activity (MainActivityWithButtons) 上按“中断”,它会打开一个页面供您输入您的地址,然后当您按下按钮时,它会发送一 strip 有特定信息的短信。信息。我需要使用 boolean 值选择该消息。如果您点击break in,它会将名为breakIn 的 boolean 值设置为true。我现在需要访问 BreakIn boolean 值和值(true 或 false,在本例中为 true),以便使用 if else 语句来选择消息。我的问题是,即使我的 boolean 值设置为 true 并且我按下按钮发送短信,我的应用程序也会崩溃!请帮助我,这是代码 fragment :

带按钮的主 Activity :

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);
}

}

位置选择警报按钮:

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)

编辑

list :

<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>

如果您需要更多信息,请随时发表评论并向我索取:) 非常感谢您的帮助!

最佳答案

通过您的 Intent 发送它们。

在 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);

关于java - 如何从不同的类访问 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31518703/

相关文章:

java - 如何在Android Studio中的RecyclerAdapter中传递多个数组

c++ - 在我的类 C++ 中创建一个对象

c++ - 返回其自身类型的模板类方法的正确签名

java - 连接被拒绝 Android studio 连接 Sql Server 显示错误

java - URL.openStream 生成错误 (javax.net.ssl.SSLException)

java - Spring 中用于 Jersey 应用程序的自定义消息正文编写器

android - 当用户多次输入错误模式时如何设置限制?

android - 有没有一种方法可以在运行时查询给定设备恰好正在使用哪个布局文件夹?

android - 扫描时如何在Android中读取蓝牙LE广告数据

css - 相同的 css 属性到不同的类/标签