0

1番目のタブの下に3つのタブを動的に生成しています GPSとWIFIの両方をオフおよびオンにできるトグルボタンを生成しています
GPSとWIFI用のJavaファイルをコーディングしました。行ごとに何度もチェックしましたが、バグは見つかりませんでした。エミュレーターとAndroid「Sony Arc mobileも」で動かしました。インストールもうまくいき、起動しようとすると問題が発生します。アプリケーションは、「アプリケーションが予期せず停止しました。もう一度やり直してください」というメッセージを表示しています。スタック オーバーフローのほぼ{すべて}のすべてのクエリを参照しましたが、シナリオに対して考えられる回答を得ることができませんでした。GPS および WIFI ファイルを削除してみました。アプリケーションでToggle ButtonTextViewに置き換えると、正常に動作します。
私のコードを含めて、それを確認して仲間を助けてください。

Main.java

パッケージ project.sow;
android.app.TabActivity をインポートします。
android.content.Intent をインポートします。
android.content.res.Resources をインポートします。
android.os.Bundle をインポートします。
android.widget.TabHost をインポートします。
public class Main extends TabActivity {
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
リソース res = getResources(); // Drawables を取得するリソース オブジェクト
tabHost = (TabHost)findViewById(android.R.id.tabhost); //アクティビティ TabHost
TabHost.TabSpec 仕様; // 各タブの再利用可能な TabSpec
// 各タブの TabSpec を初期化し、TabHost
仕様に追加します = tabHost.newTabSpec("Start").setIndicator("Start", res.getDrawable(R.drawable.ic_sample));
spec.setContent(new Intent(this,Start.class));
tabHost.addTab(仕様);
spec = tabHost.newTabSpec("Info").setIndicator("Info", res.getDrawable(R.drawable.ic_sample));
spec.setContent(new Intent().setClass(this, Info.class));
tabHost.addTab(仕様);

   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

パッケージ project.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 をインポートします。
public class Start extends Activity {
public WifiManager wifi;
ブールステータス;
コンテキスト コンテキスト。
public TextView textStatus;
public LocationManager locationManager;
パブリック LocationListener locationListener;
public BroadcastReceiver レシーバー;
パブリック LocationProvider locationProvider;

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

パッケージ project.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 をインポートします。
public class WiFiFilter extends BroadcastReceiver {
private static final String TAG = "WiFiFilter";
開始開始;
public WiFiFilter(Start 開始) {
super();
this.start=開始;
}
//非表示の SSID の準備と、後で使用するための wifi 構成の保存
@Override
public void onReceive(Context c, Intent intent) {
リスト結果 = start.wifi.getScanResults();
リストの結果 2 = null;
ScanResult フィルター = null;
//SSID のフィルタリング
for(ScanResult filterresult : results ){
if(filterresult.SSID=="VOLSBB")
{
results2 = results;}
}
//信号レベルの確認
for (ScanResult result : results2) {
if (filter == null
|| WifiManager.compareSignalLevel(filter.level, result.level) < 0)
フィルター = 結果;
試す{
WifiManager.ACTION_PICK_WIFI_NETWORK.equals(フィルター);
}
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

パッケージ project.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 をインポートします。
public class gpsLocation implements LocationListener{
Start sowWifigps;
公共の場所 currentBestLocation;
公開場所の場所。
GpsStatus ステータス;
CustomWebView webviewgps;
ダブルa;
ダブルb;
@SuppressWarnings("未使用")
プライベート文字列の最良のプロバイダー。
//public LocationManager locationManager;
public String provider1;
public gpsLocation(sowWifigps を開始) {
super();
this.sowWifigps=sowWifigps;
// TODO 自動生成されたコンストラクターのスタブ
Criteria criteria = new Criteria();
//bestProvider = sowWifigps.locationManager.getBestProvider(基準, false);
試してください{
if(LocationManager.GPS_PROVIDER==null){
provider1 = LocationManager.NETWORK_PROVIDER;
}
そうでなければ {provider1=LocationManager.GPS_PROVIDER;}
}catch( Exception e){ provider1=sowWifigps.locationManager.getBestProvider(criteria, false);}
}
@Override
public void onLocationChanged(Location location) {
// 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);
}
}

ここでは 2 番目のタブを提示しますが、他はこれと似ているため省略します。


Info.java

パッケージ project.sow;
android.app.Activity をインポートします。
android.os.Bundle をインポートします。
public class Info extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
CustomWebView webView = 新しい CustomWebView(this);
webView.loadUrl(" http://www.google.com/?s=Info ");
setContentView(webView);
super.onResume();
} }



CustomWebView.java
パッケージ project.sow;
android.app.ProgressDialog をインポートします。
android.content.Context をインポートします。
android.graphics.Bitmap をインポートします。
android.view.View をインポートします。
android.webkit.WebView をインポートします。
android.webkit.WebViewClient をインポートします。
public class CustomWebView extends WebView {
private ProgressDialog pgBar = null;
@SuppressWarnings("unused")
プライベート コンテキスト ctx;
public CustomWebView(コンテキスト コンテキスト) {
スーパー(コンテキスト);
this.clearCache(真);
this.clearFormData();
this.clearHistory();
this.getSettings().setJavaScriptEnabled(true);
this.getSettings().setUserAgentString(null);
this.requestFocus(View.FOCUS_DOWN);
this.setWebViewClient(新しい CustomWebView.CustomWebViewClient());
this.pgBar = new ProgressDialog(context);
this.pgBar.setMessage("読み込み中...");
ctx = コンテキスト;
}
プライベート クラス CustomWebViewClient extends 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) {
try{
view.loadUrl(url);
}catch(Exception ex){
}
return true;
} } }

メイン.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" />

4

1 に答える 1

0

WI-FI と GPS は、子スレッドではなくメイン スレッドで呼び出されます。
つまり、main.java では、

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

以前は start.java で初期化と呼ばれていたものを配置する

これを行うことにより、重要なプロセスが強制終了されず、アプリケーションがクラッシュするのを防ぎます。

于 2011-04-29T16:40:40.753 に答える