变量在活动之间丢失数据我不知道为什么?

时间:2016-11-16 18:47:53

标签: java android

我目前正在创建一个扫描网络并在ListView中输出结果的Android应用程序,但我正在尝试将其转到我点击网络的位置,它将数据保存在数据库中,然后将您发送到要显示的页面你选择了什么,但是当我点击一个项目时,它正确地对数值进行子字符串处理并在主要活动上显示正常工作但是当我尝试在显示页面活动上使用变量时,值被设置为null。

以下是点击监听器中的主要活动:

networklist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String grabItemInfo = wifis[position];

            Network_Info info1 = new Network_Info();

            info1.setMainBSSID( grabItemInfo.substring(grabItemInfo.indexOf('#') +1, grabItemInfo.lastIndexOf('#')));
            info1.setMainSSID( grabItemInfo.substring(0,(grabItemInfo.indexOf('#'))));
            info1.setMainCAP( grabItemInfo.substring(grabItemInfo.lastIndexOf('#')+1, grabItemInfo.length()));


            Toast toastTest = Toast.makeText(getApplicationContext(), info1.getMainSSID(), Toast.LENGTH_SHORT);
            Toast toastTest2 = Toast.makeText(getApplicationContext(), info1.getMainBSSID(), Toast.LENGTH_SHORT);
            Toast toastTest3 = Toast.makeText(getApplicationContext(), info1.getMainCAP(), Toast.LENGTH_SHORT);
            toastTest.show();
            toastTest2.show();
            toastTest3.show();

            ContentValues dbv = new ContentValues();

            dbv.put("SSID", info1.getMainSSID());
            dbv.put("BSSID", info1.getMainBSSID());
            dbv.put("CAPABILITIES", info1.getMainCAP());




            netDataBase.insert("netDataTable", "NULL", dbv);


            Intent intent = new Intent(getApplicationContext(), Attack_Page.class);
            startActivity(intent);



        }
    });

这是我的显示页面:

public class Attack_Page extends Network_List {


protected void onCreate(Bundle SavedIS){

    super.onCreate(SavedIS);
    setContentView(R.layout.attack_page);


    TextView SSIDview = (TextView) findViewById(R.id.SSIDView);
    TextView BSSIDview = (TextView) findViewById(R.id.BSSIDView);
    TextView CAPview = (TextView) findViewById(R.id.CAPView);
    Button backButton = (Button) findViewById(R.id.backbutton);
    Intent intent = getIntent();

    //String MainSSIDP = intent.getStringExtra(getMainSSID());

    Network_Info info1 = new Network_Info();

    Toast testToast = Toast.makeText(getApplicationContext(),  info1.getMainSSID(), Toast.LENGTH_SHORT);
    testToast.show();

    //Cursor IDselect = netDataBase.rawQuery("SELECT SSID FROM netDataTable WHERE SSID = "+getMainSSID()+"", wifis);

    //SSIDview.setText(IDselect.toString());


    backButton.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            Intent bintent = new Intent(getApplicationContext(), Network_List.class);
            startActivity(bintent);

        }


    });

}

}

这是我的setters和getters类:

public class Network_Info {
   private String mainCAP;
   private String mainSSID;
   private String mainBSSID;

public void setMainSSID(String newMainSSID){

    mainSSID = newMainSSID;

}

public void setMainBSSID(String newMainBSSID){

    mainBSSID = newMainBSSID;

}

public void setMainCAP(String newMainCAP){

    mainCAP = newMainCAP;

}

public String getMainSSID(){

    return mainSSID;

}
public String getMainBSSID(){

    return mainBSSID;

}
public String getMainCAP(){

    return mainCAP;

}

}

1 个答案:

答案 0 :(得分:0)

想出你必须以意图传递变量:

Intent intent = new Intent(getApplicationContext(), Attack_Page.class);
            intent.putExtra("EXTRA_SSID", info1.getMainSSID());
            intent.putExtra("EXTRA_BSSID", info1.getMainBSSID());
            intent.putExtra("EXTRA_CAP", info1.getMainCAP());
            startActivity(intent);

然后使用您在putExtra()

中设置的密钥
 String MainSSIDP = intent.getStringExtra("EXTRA_SSID");

感谢您的帮助!