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 は、actionbarsherlock を使用した MainActivity.class のフラグメントです。

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);

最終修飾子を削除できます。ListView listクラスメンバーとして宣言します。

編集された質問に回答するには

NetworkOnMainThreadExceptionUIスレッドでネットワーク関連の操作を行っているときに発生するものを取得しています。

AsyncTaskまたはを使用しthreadます。

これをチェックして

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

asynctask についてこれを確認してください

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

これ

    String xml = parser.getXmlFromUrl(URL);

バックグラウンド スレッドで実行する必要があります。

AsyncTask :

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 に答える