MySql データベースから文字列を受け取るアプリを作成しました。エミュレーターで実行すると動作しますが、デバイス (Galaxy Nexus) で実行しようとすると、強制的に閉じるだけです。エミュレーターは GB (2.2) で、私のデバイスは JB (4.2.2) です。また、許可 INTERNET をマニフェストに追加しました。
ListDBItems.java
public class VideoController extends SherlockListActivity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.dblist);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("DB ITEMS");
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://strongpixel.com/java/getDB.php");
try{
JSONArray shows = json.getJSONArray("items");
for(int i=0;i<shows.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = shows.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "" + e.getString("name"));
map.put("season", "Season: " + e.getString("season"));
map.put("path", "" + e.getString("path"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.dbitems,
new String[] { "name", "season" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(VideoController.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(VideoController.this, MainActivity.class);
myIntent.putExtra("name", o.get("name"));
myIntent.putExtra("season", o.get("season"));
myIntent.putExtra("path", o.get("path"));
startActivity(myIntent);
}
});
}
私のマニフェスト
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spxc.ssa.streaming"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Holo.Theme.Light.DarkActionBar" >
<activity
android:name="com.spxc.ssa.streaming.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.spxc.ssa.streaming.VideoController"
android:label="@string/app_name"
android:configChanges="orientation">
</activity>
<activity
android:name="com.spxc.ssa.streaming.PlayVideo"
android:label="@string/app_name" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
そして最後ですが、最後ではありませんが、logcat (NULL 例外)
03-09 13:22:15.975: E/AndroidRuntime(5141): FATAL EXCEPTION: main
03-09 13:22:15.975: E/AndroidRuntime(5141): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spxc.ssa.streaming/com.spxc.ssa.streaming.VideoController}: java.lang.NullPointerException
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.app.ActivityThread.access$600(ActivityThread.java:150)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.os.Looper.loop(Looper.java:137)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.app.ActivityThread.main(ActivityThread.java:5191)
03-09 13:22:15.975: E/AndroidRuntime(5141): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 13:22:15.975: E/AndroidRuntime(5141): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 13:22:15.975: E/AndroidRuntime(5141): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-09 13:22:15.975: E/AndroidRuntime(5141): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-09 13:22:15.975: E/AndroidRuntime(5141): at dalvik.system.NativeStart.main(Native Method)
03-09 13:22:15.975: E/AndroidRuntime(5141): Caused by: java.lang.NullPointerException
03-09 13:22:15.975: E/AndroidRuntime(5141): at com.spxc.ssa.streaming.VideoController.onCreate(VideoController.java:44)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.app.Activity.performCreate(Activity.java:5104)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-09 13:22:15.975: E/AndroidRuntime(5141): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
03-09 13:22:15.975: E/AndroidRuntime(5141): ... 11 more
ありがとう
編集: JSONfunctions.java
package com.spxc.ssa.streaming.task;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}