私は、これを手伝ってくれる 2 番目の (またはそれについては多くの) 目を得ることができるかどうか疑問に思っていました. 何がこれを引き起こしているのか、私は一生理解できません。基本的に、これは Wi-Fi の状態を変更できる単なるワイヤレス アクティビティです。しかし、私のコードは getWiFi(); の呼び出しで nullPointerException を追い出しています。123行目も指していますが、何も問題はありません。なぜこれがクラッシュするのか誰にも分かりますか? マニフェストの私の権限は、私の知る限り正しいものです。Java ファイルのコードは次のとおりです。行 89 は getWifi(); の場所です。と呼ばれます。どんな助けでも大歓迎です。
WirelessManager.java のコード
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.Button;
public class WirelessManager extends Activity {
//index values to access the elements in the TextView array.
private final int SSTRENGTH = 0;
private final int WIFISTATE = 1;
private final int IPADD = 2;
private final int MACADD = 3;
private final int SSID = 4;
private final int LINKSPD = 5;
private TextView[] data_labels;
private TextView name_label;
private TextView enable_label;
private Button state_button;
private Button back_button;
private WifiManager wifi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info_layout);
wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
TextView[] titles = new TextView[6];
data_labels = new TextView[6];
int[] left_views = {R.id.first_title, R.id.second_title, R.id.third_title,
R.id.fourth_title, R.id.fifth_title};
/*R.layout.info_layout is the same layout used for directory info.
*Re-using the layout for this activity, so id tag names may not make sense,
*but are in the correct order.
*/
int[] right_views = {R.id.dirs_label, R.id.files_label, R.id.time_stamp,
R.id.total_size, R.id.free_space};
String[] labels = {"Signal strength", "WIFI State", "ip address",
"mac address", "SSID", "link speed"};
for (int i = 0; i < 5; i++) {
titles[i] = (TextView)findViewById(left_views[i]);
titles[i].setText(labels[i]);
data_labels[i] = (TextView)findViewById(right_views[i]);
data_labels[i].setText("N/A");
}
name_label = (TextView)findViewById(R.id.name_label);
enable_label = (TextView)findViewById(R.id.path_label);
state_button = (Button)findViewById(R.id.back_button);
back_button = (Button)findViewById(R.id.zip_button);
back_button.setText(" Back ");
state_button.setOnClickListener(new ButtonHandler());
back_button.setOnClickListener(new ButtonHandler());
ImageView icon = (ImageView)findViewById(R.id.info_icon);
icon.setImageResource(R.drawable.wireless);
get_wifi();
}
private void get_wifi() {
WifiInfo info = wifi.getConnectionInfo();
int state = wifi.getWifiState();
int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
boolean enabled = wifi.isWifiEnabled();
name_label.setText(info.getSSID());
enable_label.setText(enabled ?"Your wifi is enabled" :"Your wifi is not enabled");
state_button.setText(enabled ?"Disable wifi" : "Enable wifi");
switch(state) {
case WifiManager.WIFI_STATE_ENABLED:
data_labels[WIFISTATE].setText(" Enabled");
break;
case WifiManager.WIFI_STATE_DISABLED:
data_labels[WIFISTATE].setText(" Disabled");
break;
case WifiManager.WIFI_STATE_DISABLING:
data_labels[WIFISTATE].setText(" Being Disabled");
break;
case WifiManager.WIFI_STATE_ENABLING:
data_labels[WIFISTATE].setText(" Being Enabled");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
data_labels[WIFISTATE].setText(" Unknown");
break;
}
if(enabled) {
data_labels[IPADD].setText(FileManager.integerToIPAddress(info.getIpAddress()));
data_labels[MACADD].setText(info.getMacAddress());
data_labels[SSID].setText(info.getSSID());
data_labels[LINKSPD].setText(info.getLinkSpeed() + " Mbps");
data_labels[SSTRENGTH].setText("strength " + strength);
}else {
data_labels[IPADD].setText("N/A");
data_labels[MACADD].setText(info.getMacAddress());
data_labels[SSID].setText("N/A");
data_labels[LINKSPD].setText("N/A");
data_labels[SSTRENGTH].setText("N/A");
}
}
private class ButtonHandler implements OnClickListener {
public void onClick(View v) {
if(v.getId() == R.id.back_button) {
if(wifi.isWifiEnabled()){
wifi.setWifiEnabled(false);
state_button.setText("Enable wifi");
}else {
wifi.setWifiEnabled(true);
state_button.setText("Disable wifi");
get_wifi();
}
}else if(v.getId() == R.id.zip_button)
finish();
}
}
}
Code for AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="coxxxxxxxxxx"
android:versionCode="100"
android:versionName="1.0.0"
android:installLocation="auto">
<application android:icon="@drawable/icon" android:label="@string/app_name" >
<activity android:name=".Settings" android:label="@string/app_name" />
<activity android:name=".ProcessManager" android:label="@string/manager_act" />
<activity android:name=".WirelessManager" android:label="Wireless Information" />
<activity android:name=".ApplicationBackup" android:label="Installed Applications" />
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<data android:mimeType="*/*" />
<category android:name="android.intent.category.OPENABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".AudioPlayblack"
android:label="@string/media_dialog"
android:theme="@android:style/Theme.Dialog" />
<activity android:name=".DirectoryInfo" android:enabled="true"
android:label="@string/dir_info">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="application/manager" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".HelpManager"
android:label="XXXXXXXXX"
android:theme="@android:style/Theme.Dialog" />
</application>
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true" />
<uses-sdk android:minSdkVersion="4"
android:maxSdkVersion="10"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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"/>
</manifest>