List Fragment未加载但app不会崩溃

时间:2015-01-10 18:40:43

标签: java android android-fragments loading android-listfragment

我在填充要包含列表片段的片段时遇到问题。我只是收到一条加载消息。

http://i.imgur.com/8cYSO20.png

public class MainActivity extends FragmentActivity {
Button searchBtn;
EditText searchInput;
String albumName;
boolean landscape;
MenuFragment frag2;


public static final String LOGTAG = "Internal Storage";
JSONObject apiData;

List<String> albumList = new ArrayList<String>();


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

    setContentView(R.layout.activity_main);
    File f = getFilesDir();
    String path = f.getAbsolutePath();
    Log.e(LOGTAG, path);


    searchBtn = (Button) findViewById(R.id.button1);

    searchBtn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {

         searchInput = (EditText) findViewById(R.id.editText1);

           String entry = searchInput.getText().toString();
           String newData  = entry.replaceAll(" ", "%20");


           searchInput.setText("");
           try {

               String qs = "https://ws.spotify.com/search/1/album.json?q=" + newData;
               URL queryURL = new URL(qs);

               albumList.clear();

               new GetTask().execute(queryURL);
           } catch (Exception e) {
           } 
       }
   });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


private class GetTask extends AsyncTask<URL, Integer, JSONObject> {
    final String TAG = "INFO ASYNCTASK";

    @Override
    protected JSONObject doInBackground(URL... urls) {

        String jsonString = "";

        for(URL queryURL : urls){
            try{
                URLConnection conn = queryURL.openConnection();
                jsonString = IOUtils.toString(conn.getInputStream());
                break;
            } catch (Exception e){
                Log.e(TAG, "NO URLConnection to " + queryURL.toString());

            }
        } 

        Log.i(TAG, "Received Data: " + jsonString);

        try{
            apiData = new JSONObject(jsonString);
        } catch (Exception e){
            Log.e(TAG, "Cannot change API response to JSON");
            apiData = null;
        }

        try{

             JSONArray arr = apiData.getJSONArray("albums"); 

                   albumName = arr.getJSONObject(0).getString("name");

                   for(int i =0; i < arr.length(); i++){

                       //search results 
                      albumList.add(arr.getJSONObject(i).getString("name"));

                   }

             Log.i(TAG, " JSON data imported: " + albumName.toString());
             Log.i(TAG, " JSON data imported LIST: " + albumList.toString());

         } catch (Exception e){
            Log.e(TAG, "Could not parse data record from response: " + apiData.toString());
             apiData = null;
         }
        return apiData;
    }

    protected void onPostExecute(JSONObject apiData) {
          frag2 = MenuFragment.newInstance((ArrayList<String>) albumList);
    }
 }
 }

import java.util.ArrayList;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

此我的列表片段:

public class MenuFragment extends ListFragment {
public static final String TAG = "MenuFragment.TAG";
ListView listView;
ArrayAdapter<String> adapter;

int check=0;
String spinText;
static ArrayList<String> albums;

public static MenuFragment newInstance(ArrayList<String> albumList) {
    MenuFragment frag = new MenuFragment();
    albums=albumList;
    return frag;
}
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, albums);
    }
@Override
public View onCreateView(LayoutInflater inf, ViewGroup parent, Bundle savedInstanceState) {
    View v =  inf.inflate(R.layout.listl_fragment, parent, false);
    ListView lv = (ListView) v.findViewById(R.id.list);

    lv.setAdapter(adapter);
    return v;
}
@Override
public void onListItemClick(ListView _l, View _v, int _position, long _id) {
    String albumSelection = (String)_l.getItemAtPosition(_position);
}
}

列表片段的XML listl_fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
     android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>

主要活动的XML activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
enter code here
<EditText
    android:id="@+id/editText1"
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:ems="10"
    android:hint="Search" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/editText1"

    android:text="Search" />


<fragment
    android:id="@+id/fragment1"
    android:name="android.app.ListFragment"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp" />

<fragment
    android:id="@+id/fragment2"
    android:name="com.example.ericchaneyjava2fundamentalsjan.DisplayFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="85dp" />
  </RelativeLayout>

Log Cat:

01-10 13:36:01.650: D/dalvikvm(1740): Not late-enabling CheckJNI (already on)
01-10 13:36:01.750: D/dalvikvm(1740): GC_CONCURRENT freed 71K, 5% free 3084K/3224K, paused 8ms+0ms, total 9ms
01-10 13:36:01.750: D/dalvikvm(1740): GC_FOR_ALLOC freed <1K, 5% free 3144K/3280K, paused 2ms, total 2ms
01-10 13:36:01.750: I/dalvikvm-heap(1740): Grow heap (frag case) to 3.741MB for 635808-byte allocation
01-10 13:36:01.750: D/dalvikvm(1740): GC_FOR_ALLOC freed 0K, 4% free 3765K/3904K, paused 0ms, total 0ms
01-10 13:36:01.780: D/dalvikvm(1740): GC_CONCURRENT freed <1K, 4% free 3768K/3904K, paused 20ms+0ms, total 22ms
01-10 13:36:01.780: E/Internal Storage(1740): /data/data/com.example.ericchaneyjava2fundamentalsjan/files
01-10 13:36:01.840: D/(1740): HostConnection::get() New Host Connection established 0xb97376a0, tid 1740
01-10 13:36:01.860: W/EGL_emulation(1740): eglSurfaceAttrib not implemented
01-10 13:36:01.890: D/OpenGLRenderer(1740): Enabling debug mode 0
01-10 13:36:14.820: D/dalvikvm(1740): GC_FOR_ALLOC freed 320K, 9% free 3961K/4348K, paused 3ms, total 3ms

感谢您的帮助和时间。

2 个答案:

答案 0 :(得分:1)

看起来应该是:

<fragment
    android:id="@+id/fragment1"
    android:name="com.example.ericchaneyjava2fundamentalsjan.MenuFragment"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp" />

答案 1 :(得分:0)

由于不确定的进度指示器显示,可能ListView未显示。在为ListView设置适配器后调用ListFragment.setListShown(true)

ListView lv = (ListView) v.findViewById(R.id.list);
this.setListShown(true);
lv.setAdapter(adapter);