内部ストレージ出力を解析したいのですが、常に NullPointerException が発生します
ここに私のエラーログがあります:
08-20 18:32:49.089: W/System.err(1149): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-20 18:32:49.089: W/System.err(1149): at android.os.Handler.<init>(Handler.java:197)
08-20 18:32:49.089: W/System.err(1149): at android.os.Handler.<init>(Handler.java:111)
08-20 18:32:49.089: W/System.err(1149): at android.app.Activity.<init>(Activity.java:759)
08-20 18:32:49.089: W/System.err(1149): at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:70)
08-20 18:32:49.089: W/System.err(1149): at de.everhome.cloudbox.DeviceActivity.<init>(DeviceActivity.java:54)
08-20 18:32:49.089: W/System.err(1149): at de.everhome.parser.SceneParserIS.downloadUrl(SceneParserIS.java:77)
08-20 18:32:49.089: W/System.err(1149): at de.everhome.parser.SceneParserIS.parse(SceneParserIS.java:109)
08-20 18:32:49.089: W/System.err(1149): at de.everhome.cloudbox.Fragment2$DoRssFeedTask.doInBackground(Fragment2.java:125)
08-20 18:32:49.089: W/System.err(1149): at de.everhome.cloudbox.Fragment2$DoRssFeedTask.doInBackground(Fragment2.java:1)
08-20 18:32:49.089: W/System.err(1149): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-20 18:32:49.089: W/System.err(1149): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-20 18:32:49.089: W/System.err(1149): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-20 18:32:49.089: W/System.err(1149): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-20 18:32:49.089: W/System.err(1149): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-20 18:32:49.089: W/System.err(1149): at java.lang.Thread.run(Thread.java:841)
そして、ここに解析用の私のjavaclassがあります:
package de.everhome.parser;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import de.everhome.cloudbox.DeviceActivity;
import de.everhome.model.SceneReader;
public class SceneParserIS {
private XmlPullParserFactory factory;
private XmlPullParser parser;
private List<SceneReader> SceneList;
private SceneReader rssFeed;
// private String urlString;
private String total;
private String tagName;
private String title;
private String link;
private String description;
private String category;
private String pubDate;
private String guid;
private String feedburner;
public static final String DEVICE = "scene";
public static final String DEVICES = "scenes";
public static final String DEVICENAME= "scenename";
public static final String TEXT1 ="text1";
public static final String DEVICEID = "sceneid";
public static final String FEEDBURNERORIGLINK = "feedburner:origLink";
private InputStream urlStream;
private String urlString;
public SceneParserIS(String urlString) {
this.urlString = urlString;
}
/* public SceneParserIS(Context ctx)throws IOException {
String filename = "cloudbox";
FileInputStream inputStream = ctx.openFileInput(filename);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
inputStream.close();
Log.d("File", "File contents: " + total);
String totalstring = total.toString();
InputStream totalinput = new ByteArrayInputStream(totalstring.getBytes("UTF-8"));
}*/
protected Context getContext() {
Context context = null;
return context;
}
public InputStream downloadUrl(String urlString) throws IOException {
DeviceActivity bckp = new DeviceActivity(); // send parameters if your constructor takes them
URL url = new URL(urlString);
Context ActivityContext = this.getContext();
/* URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream stream = conn.getInputStream();*/
String filename = "cloudbox";
FileInputStream inputStream = bckp.openFileInput(filename);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
inputStream.close();
Log.d("File", "File contents: " + total);
String totalstring = total.toString();
InputStream totalinput = new ByteArrayInputStream(totalstring.getBytes("UTF-8"));
return totalinput;
}
public List<SceneReader> parse() {
try {
@SuppressWarnings("unused")
int count = 0;
factory = XmlPullParserFactory.newInstance();
parser = factory.newPullParser();
urlStream = downloadUrl(urlString);
// urlStream = SceneParserIS(Context ctx);
parser.setInput(urlStream, null);
int eventType = parser.getEventType();
boolean done = false;
rssFeed = new SceneReader();
SceneList = new ArrayList<SceneReader>();
while (eventType != XmlPullParser.END_DOCUMENT && !done) {
tagName = parser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if (tagName.equals(DEVICE)) {
rssFeed = new SceneReader();
}
if (tagName.equals(DEVICENAME)) {
title = parser.nextText().toString();
}
if (tagName.equals(TEXT1)) {
category = parser.nextText().toString();
}
if (tagName.equals(DEVICEID)) {
guid = parser.nextText().toString();
}
if (tagName.equals(FEEDBURNERORIGLINK)) {
feedburner = parser.nextText().toString();
}
break;
case XmlPullParser.END_TAG:
if (tagName.equals(DEVICES)) {
done = true;
} else if (tagName.equals(DEVICE)) {
rssFeed = new SceneReader(title, link, description, category, pubDate,
guid,
feedburner);
SceneList.add(rssFeed);
}
break;
}
eventType = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d("App", "parser beendet");
return SceneList;
}
}
asynctask を持つクラス:
package de.everhome.cloudbox;
import de.everhome.model.DeviceReader;
import de.everhome.model.SceneReader;
import de.everhome.parser.SceneParser;
import de.everhome.parser.SceneParserIS;
import de.everhome.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/*public class Fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.rss_feed_view, null);
}
}*/
import java.util.ArrayList;
import java.util.List;
import com.astuetz.viewpager.extensions.PagerSlidingTabStrip;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Fragment2 extends Fragment implements OnItemClickListener {
private ListView mRssListView;
private SceneParserIS mNewsFeeder;
private List<SceneReader> mRssFeedList;
private RssAdapter mRssAdap;
private static final String TOPSTORIES =
"some url";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
View V = inflater.inflate(R.layout.rss_feed_view, container, false);
mRssListView = (ListView) V.findViewById(R.id.rss_list_view);
mRssFeedList = new ArrayList<SceneReader>();
new DoRssFeedTask().execute(TOPSTORIES);
mRssListView.setOnItemClickListener(this);
return V;
}
private class RssAdapter extends ArrayAdapter<SceneReader> {
private List<SceneReader> rssFeedLst;
public RssAdapter(Context context, int textViewResourceId, List<SceneReader> rssFeedLst) {
super(context, textViewResourceId, rssFeedLst);
this.rssFeedLst = rssFeedLst;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
RssHolder rssHolder = null;
if (convertView == null) {
Context context = getContext();
view = View.inflate(context, R.layout.scene_item, null);
rssHolder = new RssHolder();
rssHolder.rssTitleView = (TextView) view.findViewById(R.id.rss_title_view);
rssHolder.rssbuttonpositiv = (Button) view.findViewById(R.id.on);
view.setTag(rssHolder);
} else {
rssHolder = (RssHolder) view.getTag();
}
final SceneReader rssFeed = rssFeedLst.get(position);
rssHolder.rssTitleView.setText(rssFeed.getName());
rssHolder.rssbuttonpositiv.setText(rssFeed.getpositiv());
rssHolder.rssbuttonpositiv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String[] url={"http://everhome.de/api/applive/","8ef43502ad3dc04f87b4a48b993878c0",rssFeed.getdeviceid()};
//String[] url={"http://everhome.de/api/applive/","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",rssFeed.getdeviceid(),rssFeed.getaction1()};
Context context = getContext();
new run(context).execute(url);
CharSequence text = "Führe Szene durch: " + rssFeed.getdeviceid();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Log.d("App", "test");
}});
return view;
}
}
static class RssHolder {
public TextView rssTitleView;
public Button rssbuttonpositiv;
public Button rssbuttonnegativ;
}
public class DoRssFeedTask extends AsyncTask<String, Void, List<SceneReader>> {
ProgressDialog prog;
String jsonStr = null;
Handler innerHandler;
@Override
protected void onPreExecute() {
//Context context = getContext();
prog = new ProgressDialog(getActivity());
prog.setMessage("Loading....");
prog.show();
}
@Override
protected List<SceneReader> doInBackground(String... params) {
for (String urlVal : params) {
mNewsFeeder = new SceneParserIS(urlVal);
}
mRssFeedList = mNewsFeeder.parse();
return mRssFeedList;
}
@Override
protected void onPostExecute(List<SceneReader> result) {
prog.dismiss();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mRssAdap = new RssAdapter(getActivity(), R.layout.device_item,
mRssFeedList);
int count = mRssAdap.getCount();
if (count != 0 && mRssAdap != null) {
mRssListView.setAdapter(mRssAdap);
}
}
});
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
prog.dismiss();
CharSequence text = "Keine Verbindung zum Server!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getActivity(), text, duration);
toast.show();
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Geräte", "Szenen" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new Fragment1();
case 1:
return fragment = new Fragment1();
default:
break;
}
return fragment;
}
@Override
public int getCount() {
return TITLES.length;
}
}
}
コンテキスト コンテキストが原因だと思います。Context.openFileInput(ファイル名) で。