我的应用程序由两部分组成:一部分用于本地化INDOOR,另一部分用于本地化OUTDOOR。 另外,这些部件可以工作......但是当我将它们组合在一个大型应用程序中时,OUTDOOR部分不再起作用了! 我不知道为什么...... 你能帮助我吗? 我写了代码:
MAIN
package martina_cennamo.localization;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//CERCO GLI ELEMENTI BUTTON E BUTTON2
Button OutDoor = (Button) findViewById(R.id.button);
Button InDoor = (Button) findViewById(R.id.button2);
//CLICCANDO SU BUTTON FACCIO APRIRE UNA NUOVA ACTIVITY
OutDoor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//DEFINISCO L'INTENZIONE DI APRIRE UNA NUOVA ACTIVITY
Intent openActivityO=new Intent(MainActivity.this,ActivityO.class);
//APRO L'ACTIVITY ACTIVITYO.JAVA
startActivity(openActivityO);
}
});
//CLICCANDO SU BUTTON2 FACCIO APRIRE UNA NUOVA ACTIVITY
InDoor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent openActivityI=new Intent(MainActivity.this,ActivityI.class);
startActivity(openActivityI);
}
});
}
//ALTRI OVERRIDE
@Override
public boolean onCreateOptionsMenu (Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ACTIVITYO(户外)
package martina_cennamo.localization;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.List;
public class ActivityO extends AppCompatActivity {
public WifiManager wifi;
private String rete;
private String ssid1;
private String ssid2;
private String ssid3;
private int level1;
private int level2;
private int level3;
private String BSSID1;
private String BSSID2;
private String BSSID3;
private Document htmlDocument;
private TextView parsedHtmlNode;
private String html;
private TextView text;
private TextView text2;
private TextView text3;
private TextView retevicina;
Button indietro;
List<ScanResult> results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_o);
parsedHtmlNode = (TextView) findViewById(R.id.html_content);
JsoupAsyncTask jsoupAsyncTask = new JsoupAsyncTask();
jsoupAsyncTask.execute();
text = (TextView) findViewById(R.id.textView2);
text2 = (TextView) findViewById(R.id.textView3);
text3 = (TextView) findViewById(R.id.textView4);
indietro = (Button)findViewById(R.id.indietro);
retevicina = (TextView) findViewById(R.id.textView7);
//TORNA ALLA HOME
indietro = (Button)findViewById(R.id.home);
indietro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent back=new Intent(ActivityO.this,MainActivity.class);
startActivity(back);
}
});
//FINE
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class JsoupAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.startScan();
try {
Thread.sleep(3333);
} catch (InterruptedException e) {
e.printStackTrace();
}
results = wifi.getScanResults();
ScanResult result1 = results.get(0);
ssid1 = result1.SSID;
ScanResult result2 = results.get(1);
ssid2 = result2.SSID;
ScanResult result3 = results.get(2);
ssid3 = result3.SSID;
level1 = results.get(0).level;
level2 = results.get(1).level;
level3 = results.get(2).level;
BSSID1 = results.get(0).BSSID;
BSSID2 = results.get(1).BSSID;
BSSID3 = results.get(2).BSSID;
//COME PRENDO LA RETE CON LA POTENZA MAGGIORE?
if (level1 >= level2 && level1 >= level3) {
rete = ssid1;
} else {
if (level2 >= level1 && level2 >= level3) {
rete = ssid2;
} else {
if (level3 >= level1 && level3 >= level2) {
rete = ssid3;
}
}
}
//mostrare in una textview latitudine e longitudine
try {
htmlDocument = Jsoup.connect("http://geomena.org/essid/" + rete).userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22")
.timeout(30000).get();
//selezionare da html di ESSID/Vodafone - PROVA
for (Element body : htmlDocument.select("body")) {
for (Element div : body.select("div[id*=content]")) {
for (Element table : div.select("table")) {
for (Element tbody : table.select("tbody")) {
for (Element tr : tbody.select("tr:eq(0)")) {
Elements td = tr.select("td");
html = td.get(3).text();
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
text.setText("SSID: " + ssid1 + " RSSI: " + level1 + "MAC: " + BSSID1);
text2.setText("SSID: " + ssid2 + " RSSI: " + level2 + "MAC: " + BSSID2);
text3.setText("SSID: " + ssid3 + " RSSI: " + level3 + "MAC: " + BSSID3);
parsedHtmlNode.setText(html);
retevicina.setText("La rete più vicina è " + rete);
}
}
}
ACTIVITY I(INDOOR_1)
package martina_cennamo.localization;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class ActivityI extends AppCompatActivity {
Button localizza;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_i);
localizza=(Button)findViewById(R.id.button3);
localizza.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent Wifi=new Intent(ActivityI.this,ActivityWifi.class);
startActivity(Wifi);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_i, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ACTIVITY WIFI(INDOOR_2)
package martina_cennamo.localization;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class ActivityWifi extends AppCompatActivity {
public WifiManager wifi;
ImageView cucina;
ImageView soggiorno;
ImageView letto_1;
ImageView letto_2;
ImageView letto_3;
TextView posizione;
Button locate;
final Fingerprint f1= new Fingerprint();
final Fingerprint f2= new Fingerprint();
final Fingerprint f3= new Fingerprint();
final Fingerprint f4= new Fingerprint();
final Fingerprint f5= new Fingerprint();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi);
f1.setStanza("Cucina");
f2.setStanza("Soggiorno");
f3.setStanza("Letto_1");
f4.setStanza("Letto_2");
f5.setStanza("Letto_3");
for (int i=0;i<3;i++) {
f1.rete[i] = new Rete();
f2.rete[i] = new Rete();
f3.rete[i] = new Rete();
f4.rete[i] = new Rete();
f5.rete[i] = new Rete();
}
//IMPOSTO I VALORI DEI 3 WIFI PER OGNI STANZA
f1.rete[0].set_SSID("Vodafone-27661039");
f1.rete[0].set_RSSI(-85);
f1.rete[1].set_SSID("NokiaWifi");
f1.rete[1].set_RSSI(-53);
f1.rete[2].set_SSID("extender");
f1.rete[2].set_RSSI(-69);
f2.rete[0].set_SSID("Vodafone-27661039");
f2.rete[0].set_RSSI(-75);
f2.rete[1].set_SSID("NokiaWifi");
f2.rete[1].set_RSSI(-67);
f2.rete[2].set_SSID("extender");
f2.rete[2].set_RSSI(-44);
f3.rete[0].set_SSID("Vodafone-27661039");
f3.rete[0].set_RSSI(-59);
f3.rete[1].set_SSID("NokiaWifi");
f3.rete[1].set_RSSI(-79);
f3.rete[2].set_SSID("extender");
f3.rete[2].set_RSSI(-58);
f4.rete[0].set_SSID("Vodafone-27661039");
f4.rete[0].set_RSSI(-36);
f4.rete[1].set_SSID("NokiaWifi");
f4.rete[1].set_RSSI(-84);
f4.rete[2].set_SSID("extender");
f4.rete[2].set_RSSI(-70);
f5.rete[0].set_SSID("Vodafone-27661039");
f5.rete[0].set_RSSI(-57);
f5.rete[1].set_SSID("NokiaWifi");
f5.rete[1].set_RSSI(-84);
f5.rete[2].set_SSID("extender");
f5.rete[2].set_RSSI(-42);
//EFFETTUO LA SCANSIONE DEI WIFI
wifi=(WifiManager) getSystemService(Context.WIFI_SERVICE);
for (int i=0;i<5;i++){
wifi.startScan();
try {
Thread.sleep(3333);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
List<ScanResult> results = wifi.getScanResults();
if(results.size()<3){
Toast.makeText(getApplicationContext(), "Hai trovato solo " + results.size() +"reti! Scansiona di nuovo!", Toast.LENGTH_LONG).show();
finish();
}
//FINE SCANSIONE
//TORNA ALLA LOCATE
locate = (Button)findViewById(R.id.locate);
locate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent openlocate = new Intent(ActivityWifi.this, ActivityI.class);
startActivity(openlocate);
}
});
//FINE
}
@Override
protected void onResume() {
super.onResume();
cucina=(ImageView)findViewById(R.id.cucina);
soggiorno=(ImageView)findViewById(R.id.soggiorno);
letto_1=(ImageView)findViewById(R.id.letto_1);
letto_2=(ImageView)findViewById(R.id.letto_2);
letto_3=(ImageView)findViewById(R.id.letto_3);
posizione=(TextView)findViewById(R.id.posizione);
List<ScanResult> results = wifi.getScanResults();
final Rete[] retiscansione = new Rete[results.size()];
for(int i=0;i<results.size();i++){
ScanResult sr=results.get(i);
retiscansione[i]=new Rete();
retiscansione[i].set_SSID(sr.SSID);
retiscansione[i].set_RSSI(sr.level);
}
final int distanza1=f1.distanza(retiscansione);
final int distanza2=f2.distanza(retiscansione);
final int distanza3=f3.distanza(retiscansione);
final int distanza4=f4.distanza(retiscansione);
final int distanza5=f5.distanza(retiscansione);
double distanzamin = Math.min(Math.min(Math.min(distanza1, distanza2), Math.min(distanza3, distanza4)),distanza5);
if(distanzamin==distanza1){
soggiorno.setVisibility(View.INVISIBLE);
letto_1.setVisibility(View.INVISIBLE);
letto_2.setVisibility(View.INVISIBLE);
letto_3.setVisibility(View.INVISIBLE);
posizione.setText("Sei nella stanza: "+f1.getStanza());
}
if(distanzamin==distanza2){
cucina.setVisibility(View.INVISIBLE);
letto_1.setVisibility(View.INVISIBLE);
letto_2.setVisibility(View.INVISIBLE);
letto_3.setVisibility(View.INVISIBLE);
posizione.setText("Sei nella stanza: "+f2.getStanza());
}
if(distanzamin==distanza3){
cucina.setVisibility(View.INVISIBLE);
soggiorno.setVisibility(View.INVISIBLE);
letto_2.setVisibility(View.INVISIBLE);
letto_3.setVisibility(View.INVISIBLE);
posizione.setText("Sei nella stanza: "+f3.getStanza());
}
if(distanzamin==distanza4){
cucina.setVisibility(View.INVISIBLE);
soggiorno.setVisibility(View.INVISIBLE);
letto_1.setVisibility(View.INVISIBLE);
letto_3.setVisibility(View.INVISIBLE);
posizione.setText("Sei nella stanza: "+f4.getStanza());
}
if(distanzamin==distanza5){
cucina.setVisibility(View.INVISIBLE);
soggiorno.setVisibility(View.INVISIBLE);
letto_1.setVisibility(View.INVISIBLE);
letto_2.setVisibility(View.INVISIBLE);
posizione.setText("Sei nella stanza: "+f5.getStanza());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_wifi, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
FINGERPRINT CLASS
package martina_cennamo.localization;
/**
* Created by pc1 on 01/11/2015.
*/
public class Fingerprint
{
String stanza;
Rete[] rete = new Rete[3];
public int a=0;
public int b=0;
public int c=0;
public void setStanza(String stanza)
{
this.stanza = stanza;
}
public String getStanza()
{
return stanza;
}
public int distanza(Rete[] trovate){
int distanzacalcolata=0;
for(int i=0;i<trovate.length ;i++)
{
if (trovate[i].get_SSID().equals("Vodafone-27661039"))
{a=trovate[i].get_RSSI();}
if (trovate[i].get_SSID().equals("NokiaWifi"))
{b=trovate[i].get_RSSI();}
if (trovate[i].get_SSID().equals("extender"))
{c=trovate[i].get_RSSI();}
}
distanzacalcolata=(int)Math.sqrt(Math.pow(rete[0].get_RSSI()-a,2)+Math.pow(rete[1].get_RSSI()-b,2)+Math.pow(rete[2].get_RSSI()-c,2));
return distanzacalcolata;
}
}
RETE CLASS
package martina_cennamo.localization;
/**
* Created by pc1 on 01/11/2015.
*/
public class Rete {
String SSID;
int RSSI;
public String get_SSID()
{
return this.SSID;
}
public int get_RSSI()
{
return this.RSSI;
}
public void set_SSID(String SSID)
{
this.SSID=SSID;
}
public void set_RSSI(int RSSI)
{
this.RSSI=RSSI;
}
}
答案 0 :(得分:0)
我整理了你的代码,发现了户外活动中出现的两个问题。
第一个问题是indietro按钮出现空指针错误。首先将其初始化为名为R.id.indietro的按钮,然后切换到R.id.home。
indietro = (Button)findViewById(R.id.indietro);
...
//TORNA ALLA HOME
indietro = (Button)findViewById(R.id.home);
取出第二个初始化程序,它会起作用,或确保按钮名称与您想要控制的按钮相匹配。
第二个问题出现在我修复按钮之后。这是一个简单的许可问题。确保清单中列出了INTERNET权限。
<uses-permission android:name="android.permission.INTERNET" />
完成这些操作后,应用程序没有崩溃。