我已经研究了与此问题相关的所有问题,但我还没有找到解决方案。
该项目的工作原理如下:我在Web服务器上的XML文件(http://.../data.xml)中创建一个customListView(在Main2.clas中)。 XML文件中的单个条目(playStatus)在Web服务器上每15秒更新一次。我每隔15秒检查一次此应用程序的更新(在UpdateXML.class中)。如果自创建customListview以来data.xml已更新,我想更新cutomListview中的条目。
但是以下错误会返回:
java.lang.IllegalStateException:onCreate()之前的活动不可用的系统服务
Main2.class
public class Main2 extends Activity {
int playStatus;
String Adi,online ,Logo ,msUrlx ;
public int ax;
ArrayList<String> onlineKontrol;
public ListView defaultList;
@Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
defaultList = (ListView) findViewById(R.id.listview5);
CustomAdapter myAdapter = new CustomAdapter(this,MainListOlustur());
defaultList.setAdapter(myAdapter);
UpdateXML guncelle = new UpdateXML();
guncelle.execute();
}
public ArrayList<MainList> MainListOlustur(){
ArrayList<MainList> defaultList = new ArrayList<MainList>();
try {
URL url = new URL("http://.../data.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("kanal");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList adiList = fstElmnt.getElementsByTagName("adi");
Element adiElement = (Element) adiList.item(0);
adiList = adiElement.getChildNodes();
NodeList logoList = fstElmnt.getElementsByTagName("logo");
Element logoElement = (Element) logoList.item(0);
logoList = logoElement.getChildNodes();
NodeList msUrlList = fstElmnt.getElementsByTagName("msurl");
Element msUrlElement = (Element) msUrlList.item(0);
msUrlList = msUrlElement.getChildNodes();
NodeList onlineList = fstElmnt.getElementsByTagName("online");
Element onlineElement = (Element) onlineList.item(0);
onlineList = onlineElement.getChildNodes();
NodeList playStatusList = fstElmnt.getElementsByTagName("playStatus");
Element playStatusElement = (Element) playStatusList.item(0);
playStatusList = playStatusElement.getChildNodes();
Adi = ((Node) adiList.item(0)).getNodeValue();
online = ((Node) onlineList.item(0)).getNodeValue();
Logo = ((Node) logoList.item(0)).getNodeValue();
msUrlx = ((Node) msUrlList.item(0)).getNodeValue();
playStatus = Integer.parseInt(((Node) playStatusList.item(0)).getNodeValue());
defaultList.add(new MainList(Adi,online, Logo, msUrlx,playStatus));
}
} catch (Exception e) {
System.out.println("XML READING ERROR-MAIN = " + e);
}
return defaultList;
}
public void setCheck(int a, String playStatus) {
int mainListViewResult = Main2.this.MainListOlustur().get(a).getPlayStatus();
Log.e("DURUM",a+") "+playStatus+"-"+mainListViewResult);
if (mainListViewResult != Integer.parseInt(playStatus))
{
Log.w("DURUM",a+"-"+playStatus+"-"+mainListViewResult);
MainListOlustur().set(a,MainListOlustur().get(a)).setPlayStatus(View.VISIBLE);
CustomAdapter myAdapter2 = new CustomAdapter(this,MainListOlustur());
defaultList.setAdapter(myAdapter2);
myAdapter2.notifyDataSetChanged();
}
}
}
UpdateXML.class
public class UpdateXML extends AsyncTask{
private Handler mHandler = new Handler();
private void startTime() {
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 15000);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
mHandler.postDelayed(this, 15000);
try {
URL url2 = new URL("http://.../data.xml");
DocumentBuilderFactory dbf2 = DocumentBuilderFactory.newInstance();
DocumentBuilder db2 = dbf2.newDocumentBuilder();
Document doc2 = db2.parse(new InputSource(url2.openStream()));
doc2.getDocumentElement().normalize();
NodeList nodeList2 = doc2.getElementsByTagName("kanal");
Main2 runC = new Main2();
for (int iXml = 0; iXml < nodeList2.getLength(); iXml++) {
Node node2 = nodeList2.item(iXml);
Element fstElmnt2 = (Element) node2;
NodeList playStatusList2 = fstElmnt2.getElementsByTagName("playStatus");
Element playStatusElement2 = (Element) playStatusList2.item(0);
playStatusList2 = playStatusElement2.getChildNodes();
String playStatus2 = playStatusList2.item(0).getNodeValue();
runC.setCheck(iXml,playStatus2);
// Log.e("DURUM","XML L = "+iXml+") " + playStatus2);
}
} catch (Exception e) {
Log.e("DURUM","XML READING ERROR-UpdateXML = " + e);
}
}
};
}
CustomAdapter.class
public class CustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private java.util.List mListListesi;
private String listeler[];
TextView textViewOnline;
MainList mainList;
String streamURL;
int playStatus;
public CustomAdapter(Activity activity, java.util.List kisiler) {
mInflater = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mListListesi = kisiler;
}
public void updateList(java.util.List kisiler) {
kisiler.clear();
kisiler.addAll(kisiler);
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return mListListesi.size();
}
@Override
public MainList getItem(int position) {
return (MainList) mListListesi.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View satirView;
satirView = mInflater.inflate(R.layout.satir_layout, null);
TextView textViewKanalAdi = (TextView) satirView.findViewById(R.id.kanalAdi);
textViewOnline = (TextView) satirView.findViewById(R.id.online);
final ImageButton playBtn = (ImageButton) satirView.findViewById(R.id.playBtn);
ProgressBar loader = (ProgressBar) satirView.findViewById(R.id.playStatus);
final TextView fullText = (TextView) satirView.findViewById(R.id.fullText);
final Long itemID;
itemID=getItemId(position);
mainList = (MainList) mListListesi.get(position);
listeler = new String[]{mainList.getkanalAdi()};
streamURL = mainList.getMsurl();
playStatus= mainList.getPlayStatus();
textViewKanalAdi.setText(mainList.getkanalAdi());
textViewOnline.setText(mainList.getOnline());
return satirView;
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.net.tv">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.Main2"
/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TV"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TvFull"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.TvFull" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
TvFull.class
public class TvFull extends Activity{
private static final String TAG = null;
VideoView videoView, videoView22;
ProgressDialog progDailog;
String myuri,msurlPlayer,str;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.tvfull);
Bundle degiskenler=getIntent().getExtras();
if(degiskenler != null){
msurlPlayer = degiskenler.getString("msurl");
Log.e("---msurl---",msurlPlayer);
}
else{
Log.e("---msurl---","NULLLLLLL !!");
}
videoView = (VideoView)findViewById(R.id.videoView2);
myuri=msurlPlayer;
videoView.setVideoPath(myuri);
videoView.requestFocus();
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
videoView.start();
progDailog = ProgressDialog.show(this, "", "Loading...", true);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progDailog.dismiss();
}
});
}
else {
new AlertDialog.Builder(this)
.setTitle("Connection error !")
.setMessage("The device must have an internet connection to use the application!")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
};
;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
final TextView txt1 = (TextView) findViewById(R.id.textViewBaglantiKontrol);
videoView22 = (VideoView)findViewById(R.id.videoView2);
if(ev.getAction() == MotionEvent.ACTION_DOWN){
Log.d(TAG, "ACTION_DOWN");
if (videoView22.isPlaying()){
videoView22.pause();
txt1.setText("Stopped");
final Handler h = new Handler();
h.postDelayed(new Runnable()
{
private long time = 0;
@Override
public void run()
{
time += 1000;
if(time==3000){
Log.d("Timer", "OK!" + time);
txt1.setText("");
}
else{
h.postDelayed(this, 1000);
}
;
}
}, 1000);
}
else if(videoView22.isPlaying()==false) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
videoView22.start();
txt1.setText("Playing");
final Handler h = new Handler();
h.postDelayed(new Runnable()
{
private long time = 0;
@Override
public void run()
{
time += 1000;
Log.d("Timer", "Player Notification:" + time);
if(time==3000){
Log.d("Timer", "OK !" + time);
txt1.setText("");
}
else{
h.postDelayed(this, 1000);
}
;
}
}, 1000);
}
else {
new AlertDialog.Builder(this)
.setTitle("Connection error!")
.setMessage("The device must have an internet connection to use the application!")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
};
}
}
return super.onTouchEvent(ev);
}
@Override
public void onDestroy() {
videoView.stopPlayback();
super.onDestroy();
}
}