【发布时间】:2011-08-14 20:37:57
【问题描述】:
在第一个选项卡下动态生成 3 个选项卡正在生成一个切换按钮,我可以使用它来关闭和打开 GPS 和 WIFI
我已经为 GPS 和 WIFI 编写了 java 文件。我一次又一次地逐行检查,但找不到任何错误。
我已经在模拟器和android“Sony Arc mobile”中运行过。安装也很顺利,当我尝试启动时出现问题。
应用程序显示消息“应用程序已意外停止,请重试”我已经参考了几乎{所有}堆栈溢出中的所有查询并且无法获得任何可能回答我的场景..
我尝试删除应用程序中的 GPS 和 WIFI 文件并将 Toggle Button 替换为 TextView 然后它工作正常。
我包括我的代码,请验证它并帮助我。
Main.java
打包项目.sow;
导入 android.app.TabActivity;
导入 android.content.Intent;
导入 android.content.res.Resources;
导入 android.os.Bundle;
导入 android.widget.TabHost;
公共类 Main 扩展 TabActivity {
私人 TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
试试{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
资源 res = getResources(); // 获取Drawables的资源对象
tabHost = (TabHost)findViewById(android.R.id.tabhost); //Activity TabHost
TabHost.TabSpec 规范; // 每个选项卡的可重用 TabSpec
// 为每个选项卡初始化一个 TabSpec 并将其添加到 TabHost
规格 = tabHost.newTabSpec("开始").setIndicator("开始", res.getDrawable(R.drawable.ic_sample));
spec.setContent(new Intent(this,Start.class));
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Info").setIndicator("Info", res.getDrawable(R.drawable.ic_sample));
spec.setContent(new Intent().setClass(this, Info.class));
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Update").setIndicator("Update", res.getDrawable(R.drawable.ic_sample));
spec.setContent(new Intent(this, Update.class));
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}catch(Exception e)
{
android.util.Log.i("PREFS",e.getMessage());
} } }
Start.java
打包项目.sow;
导入 android.app.Activity;
导入 android.content.BroadcastReceiver;
导入 android.content.Context;
导入 android.content.IntentFilter;
导入 android.location.LocationListener;
导入 android.location.LocationManager;
导入 android.location.LocationProvider;
导入 android.net.wifi.WifiManager;
导入 android.os.Bundle;
导入 android.widget.TextView;
导入 android.widget.Toast;
导入 android.widget.ToggleButton;
公共类开始扩展活动{
公共 WifiManager wifi;
布尔状态;
上下文上下文;
公共 TextView 文本状态;
公共位置管理器位置管理器;
public LocationListener locationListener;
公共广播接收器接收器;
公共位置提供者位置提供者;
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Toggle to On r Off the Application");
setContentView(textview);
ToggleButton btnonoff = new ToggleButton(this);
setContentView(btnonoff);
if(btnonoff.isChecked()){
status=true;
wifi.setWifiEnabled(true);
try{
// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (receiver == null)
receiver = new WiFiFilter(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
if (locationListener ==null ){locationListener=new gpsLocation(this);}
}
catch(Exception e){Toast.makeText(context, WIFI_SERVICE, Toast.LENGTH_LONG).show();}
}
else if(!btnonoff.isChecked()) {
status=false;
if(wifi.isWifiEnabled()){
wifi.setWifiEnabled(false);
locationManager.removeUpdates((LocationListener) this);
}
}
}
catch(Exception e)
{
android.util.Log.i("PREFS",e.getMessage());
}
} }
WifiFilter.java
打包项目.sow;
导入 java.util.List;
导入 android.content.BroadcastReceiver;
导入 android.content.Context;
导入 android.content.Intent;
导入 android.net.wifi.ScanResult;
导入 android.net.wifi.WifiManager;
导入 android.util.Log;
导入 android.widget.Toast;
公共类 WiFiFilter 扩展 BroadcastReceiver {
私有静态最终字符串 TAG = "WiFiFilter";
开始开始;
公共 WiFiFilter(开始开始) {
超级();
this.start=开始;
}
//准备隐藏SSID并保存wifi配置以备后用
@Override
public void onReceive(Context c, Intent intent) {
列出结果 = start.wifi.getScanResults();
列出结果2 = null;
扫描结果过滤器 = null;
//过滤SSID
for(ScanResult filterresult : 结果){
if(filterresult.SSID=="VOLSBB")
{
结果2 = 结果;}
}
//检查信号电平
for (ScanResult 结果 : results2) {
if (filter == null
|| WifiManager.compareSignalLevel(filter.level, result.level) 过滤器 = 结果;
试试{
WifiManager.ACTION_PICK_WIFI_NETWORK.equals(filter);
}
catch(Exception e){String res="Exception:No network";Toast.makeText(start, res, Toast.LENGTH_LONG).show();};
}
String message = String.format("连接到 S0W");
if(filter.SSID != null){
//Toast.makeText(start, message, Toast.LENGTH_LONG).show();
Log.d(TAG, "onReceive() 消息:" + 消息);
}
} }
gpsLocation.java
打包项目.sow;
导入 project.sow.CustomWebView;
导入 android.location.Criteria;
导入 android.location.GpsStatus;
导入 android.location.Location;
导入 android.location.LocationListener;
导入 android.location.LocationManager;
导入 android.os.Bundle;
导入 project.sow.Start;
公共类 gpsLocation 实现 LocationListener{
启动 sowWifigps;
公共位置 currentBestLocation;
公共位置位置;
GpsStatus 状态;
CustomWebView webviewgps;
双a;
双b;
@SuppressWarnings("未使用")
私有字符串 bestProvider;
//public LocationManager locationManager;
公共字符串 provider1;
公共 gpsLocation(开始播种Wifigps) {
超级();
this.sowWifigps=sowWifigps;
// TODO 自动生成的构造函数存根
标准标准 = 新标准();
//bestProvider = sowWifigps.locationManager.getBestProvider(criteria, false);
试试{
if(LocationManager.GPS_PROVIDER==null){
provider1 = LocationManager.NETWORK_PROVIDER;
}
否则 {provider1=LocationManager.GPS_PROVIDER;}
}catch( 异常 e){ provider1=sowWifigps.locationManager.getBestProvider(criteria, false);}
}
@Override
public void onLocationChanged(位置位置) {
// TODO 自动生成的方法存根
sowWifigps.locationManager.requestLocationUpdates(provider1, 12000, 200, sowWifigps.locationListener);
}
@Override
public void onProviderDisabled(String provider) {
// TODO 自动生成的方法存根
//provider = LocationManager.NETWORK_PROVIDER;
currentBestLocation=sowWifigps.locationManager.getLastKnownLocation(provider1);
a=currentBestLocation.getLatitude();
b=currentBestLocation.getLongitude();
webviewgps.loadUrl("http://localhost/mobile/default_mobile.php?lat="+a+"&long="+b); //更改为站点名称
}
@Override
public void onProviderEnabled(String provider) {
// TODO 自动生成的方法存根
sowWifigps.locationManager.requestLocationUpdates(provider1, 12000, 200, sowWifigps.locationListener);
a=currentBestLocation.getLatitude();
b=currentBestLocation.getLongitude();
webviewgps.loadUrl("http://localhost/mobile/default_mobile.php?lat="+a+"&long="+b); //更改为站点名称
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO 自动生成的方法存根
sowWifigps.locationListener.onLocationChanged(location);
}
}
我在这里展示了第二个标签,其他的和这个相似。所以我省略了它。
Info.java
打包项目.sow;
导入 android.app.Activity;
导入 android.os.Bundle;
公共类信息扩展了 Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
受保护的无效 onResume() {
CustomWebView webView = new CustomWebView(this);
webView.loadUrl("http://www.google.com/?s=Info");
setContentView(webView);
超级.onResume();
} }
CustomWebView.java
打包 project.sow;
导入 android.app.ProgressDialog;
导入 android.content.Context;
导入 android.graphics.Bitmap;
导入 android.view.View;
导入 android.webkit.WebView;
导入 android.webkit.WebViewClient;
公共类 CustomWebView 扩展 WebView {
私有 ProgressDialog pgBar = null;
@SuppressWarnings("未使用")
私有上下文 ctx;
公共 CustomWebView(上下文上下文){
超级(上下文);
this.clearCache(true);
this.clearFormData();
this.clearHistory();
this.getSettings().setJavaScriptEnabled(true);
this.getSettings().setUserAgentString(null);
this.requestFocus(View.FOCUS_DOWN);
this.setWebViewClient(new CustomWebView.CustomWebViewClient());
this.pgBar = new ProgressDialog(context);
this.pgBar.setMessage("加载中...");
ctx = 上下文;
}
私有类 CustomWebViewClient 扩展 WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (!CustomWebView.this.pgBar.isShowing()) {
CustomWebView.this.pgBar.show();
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (CustomWebView.this.pgBar.isShowing()) {
CustomWebView.this.pgBar.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
试试{
view.loadUrl(url);
}catch(异常前){
}
返回真;
} } }
main.XML
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com /apk/res/android" android:id="@android:id/tabhost" android:background="#cabfa9">
<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="#1b3b5b" ></TabWidget>
<FrameLayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbars="horizontal" android:background="#cabfa9">
</FrameLayout>
</LinearLayout>
</TabHost>
style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="tabText" parent="@android:style/Theme.Black">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">10dip</item>
</style>
</resources>
`
ic_sample
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/icon1"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/icon2" />
</selector>
我在清单文件中添加了以下权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
【问题讨论】:
标签: android gps tabwidget android-wifi