W
WolfCH
Ambitioniertes Mitglied
- 1
Hallo Zusammen
Ich habe eine App mit Fragmenten und verschiedenen Layouts.
Da ich bei einem Layout nun Daten aus einer SQL-DB auslesen möchte via JSON und diese im ListView anzeigen lassen will, habe ich eine Listview erstellt.
Da es nicht möglich ist bei der Fragmentklasse einfach extends Activity zu extends ListActivity abzuändern, habe ich eine neue Klasse "FragmentListActivity" erstellt, welche dann ListActivity extended.
Diese dann eingebunden mittels "public class MainActivity extends FragmentListActivity". Nun möchte ich alles in der ListView anzeigen, doch die App beendet sich schon beim Start wieder und ich erhalte folgende Fehlermeldung:
Die ListView hat aber die id "list", wie man hier sieht:
Hier die Klasse 'FragmentListActivity':
Der Code im Fragment wo die Daten ausliest:
Aber ich denke der Fehler kommt von der Klasse.
Wieso ich immer 'Your content must have a ListView whose id attribute is 'android.R.id.list'' erhalte, obwohl die ID vorhanden ist, ist mir ein rätsel.
Jemand eine Idee??
Ebenso erhalte ich auch die Fehlermeldung: 'Fehler bei der http Verbindung'.... obwohl Permission INTERNET gesetzt und auch die Datei komplett ok ist, denn ich erhalte alle Ergebnise als json zurück, wie hier zu sehen ist: http://www.ssv-ufs.ch/app/spl_db_android.php
Ich habe eine App mit Fragmenten und verschiedenen Layouts.
Da ich bei einem Layout nun Daten aus einer SQL-DB auslesen möchte via JSON und diese im ListView anzeigen lassen will, habe ich eine Listview erstellt.
Da es nicht möglich ist bei der Fragmentklasse einfach extends Activity zu extends ListActivity abzuändern, habe ich eine neue Klasse "FragmentListActivity" erstellt, welche dann ListActivity extended.
Diese dann eingebunden mittels "public class MainActivity extends FragmentListActivity". Nun möchte ich alles in der ListView anzeigen, doch die App beendet sich schon beim Start wieder und ich erhalte folgende Fehlermeldung:
04-20 20:19:17.399 1422-1422/com.stormchase.ssv_ufsig.ch E/AndroidRuntime FATAL EXCEPTION: main
Process: com.stormchase.ssv_ufsig.ch, PID: 1422
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stormchase.ssv_ufsig.ch/com.stormchase.ssv_ufsig.ch.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.stormchase.ssv_ufsig.ch.FragmentListActivity.onContentChanged(FragmentListActivity.java:231)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
at android.app.Activity.setContentView(Activity.java:1929)
at com.stormchase.ssv_ufsig.ch.MainActivity.onCreate(MainActivity.java:146)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
************at android.app.ActivityThread.access$800(ActivityThread.java:135)
************at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
************at android.os.Handler.dispatchMessage(Handler.java:102)
************at android.os.Looper.loop(Looper.java:136)
************at android.app.ActivityThread.main(ActivityThread.java:5017)
************at java.lang.reflect.Method.invokeNative(Native Method)
Die ListView hat aber die id "list", wie man hier sieht:
PHP:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/list"/>
Hier die Klasse 'FragmentListActivity':
PHP:
package com.stormchase.ssv_ufsig.ch;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
public class FragmentListActivity extends ListActivity {
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected ListAdapter mAdapter;
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected ListView mList;
private Handler mHandler = new Handler();
private boolean mFinishedStart = false;
private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};
/**
* This method will be called when an item in the list is selected.
* Subclasses should override. Subclasses can call
* getListView().getItemAtPosition(position) if they need to access the
* data associated with the selected item.
*
* @param l The ListView where the click happened
* @param v The view that was clicked within the ListView
* @param position The position of the view in the list
* @param id The row id of the item that was clicked
*/
protected void onListItemClick(ListView l, View v, int position, long id) {
}
/**
* Ensures the list view has been created before Activity restores all
* of the view states.
*
*@see Activity#onRestoreInstanceState(Bundle)
*/
@Override
protected void onRestoreInstanceState(Bundle state) {
ensureList();
super.onRestoreInstanceState(state);
}
/**
* Updates the screen state (current list and other views) when the
* content changes.
*
* @see Activity#onContentChanged()
*/
@Override
public void onContentChanged() {
super.onContentChanged();
View emptyView = findViewById(android.R.id.list);
mList = (ListView)findViewById(android.R.id.list);
if (mList == null) {
throw new RuntimeException(
"Your content must have a ListView whose id attribute is " +
"'android.R.id.list'");
}
if (emptyView != null) {
mList.setEmptyView(emptyView);
}
mList.setOnItemClickListener(mOnClickListener);
if (mFinishedStart) {
setListAdapter(mAdapter);
}
mHandler.post(mRequestFocus);
mFinishedStart = true;
}
/**
* Provide the cursor for the list view.
*/
public void setListAdapter(ListAdapter adapter) {
synchronized (this) {
ensureList();
mAdapter = adapter;
mList.setAdapter(adapter);
}
}
/**
* Set the currently selected list item to the specified
* position with the adapter's data
*
* @param position
*/
public void setSelection(int position) {
mList.setSelection(position);
}
/**
* Get the position of the currently selected list item.
*/
public int getSelectedItemPosition() {
return mList.getSelectedItemPosition();
}
/**
* Get the cursor row ID of the currently selected list item.
*/
public long getSelectedItemId() {
return mList.getSelectedItemId();
}
/**
* Get the activity's list view widget.
*/
public ListView getListView() {
ensureList();
return mList;
}
/**
* Get the ListAdapter associated with this activity's ListView.
*/
public ListAdapter getListAdapter() {
return mAdapter;
}
private void ensureList() {
if (mList != null) {
return;
}
setContentView(com.stormchase.ssv_ufsig.ch.R.layout.fragment_spl);
}
private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
onListItemClick((ListView)parent, v, position, id);
}
};
}
Der Code im Fragment wo die Daten ausliest:
PHP:
private ListView lv;
InputStream is;
ArrayList<String> results = new ArrayList<String>();
org.json.JSONObject json_data;
public void getData() {
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.ssv-ufs.ch/app/spl_db_android.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Fehler bei der http Verbindung " + e.toString());
}
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{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
results.add((String) json_data.get("name") + " " + json_data.get("ort"));
}
fillList();
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
public void fillList() {
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.fragment_spl, results));
}
Aber ich denke der Fehler kommt von der Klasse.
Wieso ich immer 'Your content must have a ListView whose id attribute is 'android.R.id.list'' erhalte, obwohl die ID vorhanden ist, ist mir ein rätsel.
Jemand eine Idee??
Ebenso erhalte ich auch die Fehlermeldung: 'Fehler bei der http Verbindung'.... obwohl Permission INTERNET gesetzt und auch die Datei komplett ok ist, denn ich erhalte alle Ergebnise als json zurück, wie hier zu sehen ist: http://www.ssv-ufs.ch/app/spl_db_android.php