1

最终 ListView = (ListView) v.findViewById(R.id.game_list);

根据 Android Studio 的说法,这不是一个声明,我不确定为什么,因为它对我来说看起来不错。也许它在文件中的位置不正确,但我确定不是。它只是说这不是一个让我烦恼的声明。这是它所在的代码。

CustomizedListView.class:

public class CustomizedListView extends Fragment {
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";

ListView list;
LazyAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    final View v = inflater.inflate(R.layout.list_row, container, false);
    final ListView = (ListView) v.findViewById(R.id.game_list);
    return v;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

        // adding HashList to ArrayList
        songsList.add(map);
    }




    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(getActivity(), songsList);
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {


        }
    });
}
}

game_list 在 main.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e5e5e5" >

<ListView
    android:id="@+id/game_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#e5e5e5"
    android:dividerHeight="6dp"
    android:background="#e5e5e5"
    android:layout_marginLeft="6dp"
    android:layout_marginRight="6dp"/>

</LinearLayout>

CustomizedListView 是 MainActivity.class 的一个片段,使用 actionbarsherlock:

public class MainActivity extends SherlockFragmentActivity {

private ViewPager mViewPager;
private TabSwipe mTabSwipe;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);
    setContentView(mViewPager);

    final ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mTabSwipe = new TabSwipe(this, mViewPager);
    mTabSwipe.addTab(bar.newTab().setText("Games"), CustomizedListView.class, null);
    mTabSwipe.addTab(bar.newTab().setText("Movies"), Purchase_Fragment.class, null);
    mTabSwipe.addTab(bar.newTab().setText("Null"), Purchase_Fragment.class, null);


    }
}

修复后的Logcat:

07-20 16:36:41.635    1649-1649/com.launchpadsw.releasedates E/Trace: error opening trace file: No such file or directory (2)
07-20 16:36:41.899    1649-1649/com.launchpadsw.releasedates E/AndroidRuntime: FATAL EXCEPTION: main
    android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    at java.net.InetAddress.getAllByName(InetAddress.java:214)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    at com.launchpadsw.releasedates.XMLParser.getXmlFromUrl(XMLParser.java:45)
    at com.launchpadsw.releasedates.CustomizedListView.onCreate(CustomizedListView.java:49)
    at android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
    at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)
    at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:801)
    at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1062)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15518)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

你已经有了这个

     ListView list;

替换这个

      final ListView = (ListView) v.findViewById(R.id.game_list);

经过

      list = (ListView) v.findViewById(R.id.game_list);

您可以删除 final 修饰符。声明ListView list为类成员。

回答编辑的问题

NetworkOnMainThreadException当您在 ui 线程上执行与网络相关的操作时,您会遇到这种情况。

使用AsyncTaskthread

检查这个

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

检查这个异步任务

http://developer.android.com/reference/android/os/AsyncTask.html

这个

    String xml = parser.getXmlFromUrl(URL);

应该在后台线程上完成。

异步任务:

class TheTask extends AsyncTask<Void,Void,Void>
{


@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
}

@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
             xml= parser.getXmlFromUrl(URL);
    return null;
}

}

调用

  new TheTask().execute();

声明String xml;为类成员。

于 2013-07-20T16:29:25.193 回答