その問題を調査しましたが、コードの解決策が見つかりません。私は4つのクラスを持っています。しかし、Connect という名前のクラスには問題がないと確信しています (以下のクラス JsonData にそのオブジェクトが表示されます)。それで、他の 3 つのクラスを共有します。私の MainActivity は;
public class MainActivity extends Activity {
String keys[] = {"Message", "Subject", "MessageType", "SentTime", "ToName", "Id"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JsonData.keys=keys;
JsonData jsonData = new JsonData();
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
dataList= jsonData.GetData();
}
}
json のキー値を送信し、クラス JsonData の任意の URL (非公開です。削除します) からデータを取得します。
public class JsonData extends Activity{
Connect connect = new Connect();
private String url = "url";
static String keys[];
public ArrayList<HashMap<String, String>> GetData() {
JSONArray json = null;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
InternetCheck netCheck = new InternetCheck();
if(netCheck.isInternetOn()){
try {
JSONObject result = connect
.connect(url);
json = result.getJSONArray("d");
for (int i = 0; i < json.length(); i++) {
HashMap<String, String> hashmap = new HashMap<String, String>();
JSONObject json2 = json.getJSONObject(i);
for (int j = 0; j < keys.length; j++){
hashmap.put(keys[j], json2.getString(keys[j]));
}
mylist.add(hashmap);
}
} catch (Exception e) {
e.printStackTrace();
}
}
else
Toast.makeText(this, " Check Internet Options. ", Toast.LENGTH_LONG).show();
return mylist;
}
}
このクラスでは、オブジェクト netCheck からインターネット接続をチェックします。私の問題はそのクラスにあると思います。InternetCheck.java ;
public class InternetCheck extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public final boolean isInternetOn() {
// get Connectivity Manager object to check connection
ConnectivityManager connec = (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
// Check for network connections
if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
return true;
} else if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) {
return false;
}
return false;
}
}
コメント行 **//get Connectivity Manager object to check connection ** から疑っています。最後に私のLogcat;
08-27 10:13:45.449: E/AndroidRuntime(24925): FATAL EXCEPTION: main
08-27 10:13:45.449: E/AndroidRuntime(24925): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonparser/com.example.jsonparser.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.os.Looper.loop(Looper.java:130)
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.app.ActivityThread.main(ActivityThread.java:3768)
08-27 10:13:45.449: E/AndroidRuntime(24925): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 10:13:45.449: E/AndroidRuntime(24925): at java.lang.reflect.Method.invoke(Method.java:507)
08-27 10:13:45.449: E/AndroidRuntime(24925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
08-27 10:13:45.449: E/AndroidRuntime(24925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
08-27 10:13:45.449: E/AndroidRuntime(24925): at dalvik.system.NativeStart.main(Native Method)
08-27 10:13:45.449: E/AndroidRuntime(24925): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.app.Activity.getSystemService(Activity.java:3536)
08-27 10:13:45.449: E/AndroidRuntime(24925): at com.example.jsonparser.InternetCheck.isInternetOn(InternetCheck.java:19)
08-27 10:13:45.449: E/AndroidRuntime(24925): at com.example.jsonparser.JsonData.GetData(JsonData.java:25)
08-27 10:13:45.449: E/AndroidRuntime(24925): at com.example.jsonparser.MainActivity.onCreate(MainActivity.java:21)
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-27 10:13:45.449: E/AndroidRuntime(24925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
繰り返しますが、他の質問を調べましたが、解決策がありません。前もって感謝します。