2013-01-11 31 views
0

我是新手Android開發者。我的項目目標是製作一款下載應用程序。我決定用碎片來做到這一點。我得到了一個N​​PE,並且我還沒有發現什麼問題。 這是我的代碼:爲什麼我在customarrayadapter中出現空點異常? +孩子的家長問題

activity_main.xml中

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <android.support.v4.view.PagerTitleStrip 
     android:id="@+id/pager_title_strip" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:background="#33b5e5" 
     android:paddingBottom="4dp" 
     android:paddingTop="4dp" 
     android:textColor="#fff" 
     /> 

</android.support.v4.view.ViewPager> 

list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:id="@+id/dlist" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:focusable="false" > 
    </ListView> 


</RelativeLayout> 

row.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/downloadFileName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 

     android:text="FILENAME" /> 

    <TextView 
     android:id="@+id/percent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="%" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/time" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/downloadFileName" 
     android:text="time" /> 

    <TextView 
     android:id="@+id/data" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/time" 
     android:text="data" /> 

    <TextView 
     android:id="@+id/speed" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/percent" 
     android:text="speed" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <ProgressBar 
     android:id="@+id/downloadProgressBar" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/speed" /> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends FragmentActivity { 

    SectionsPagerAdapter mSectionsPagerAdapter; 
    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     this.setTitle(null); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
     mViewPager.setOffscreenPageLimit(2); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      switch(position){ 
      case 0: 
       Fragment fragment = new DownloadingFragment(); 
       Bundle args = new Bundle(); 
       args.putInt(DownloadingFragment.ARG_SECTION_NUMBER, position + 1); 
       fragment.setArguments(args); 
       return fragment; 
      case 1: 
       Fragment fragment2 = new FinishedFragment(); 
       Bundle args2 = new Bundle(); 
       args2.putInt(FinishedFragment.ARG_SECTION_NUMBER, position + 1); 
       fragment2.setArguments(args2); 
       return fragment2; 
      } 
      return null; 
     } 

     @Override 
     public int getCount() { 
      return 2; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(); 

      } 
      return null; 
     } 
    } 
    public static class DownloadingFragment extends ListFragment { 

     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DownloadingFragment() { 
     } 

     @Override 
     public View onCreateView (LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View V = inflater.inflate(R.layout.list, container, false); 
      ListView dlistView = (ListView) V.findViewById(android.R.id.list); 
      ((ViewGroup)container.getParent()).removeView(container); 
      List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>(); 
      downloadInfo.add(new DownloadInfo("File", 1000)); 
      DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(getActivity().getApplication(),android.R.id.list, downloadInfo); 

      dlistView.setAdapter(diaa);// Null point exception line 142! 

     return dlistView; 
     } 
    } 
    public static class FinishedFragment extends Fragment { 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public FinishedFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      // Create a new TextView and set its text to the fragment's section 
      // number argument value. 
      TextView textView = new TextView(getActivity()); 
      textView.setGravity(Gravity.CENTER); 
      textView.setText(Integer.toString(getArguments().getInt(
        ARG_SECTION_NUMBER))); 
      return textView; 
     } 
    } 

} 

Downloadinfo.java

public class DownloadInfo { 
    public enum DownloadState { 
     NOT_STARTED, DOWNLOADING, COMPLETE 
    } 

    public DownloadInfo(String filename, Integer size) { 
     super(); 
     mFilename = filename; 
     mProgress = 0; 
     mFileSize = size; 
     mProgressBar = null; 
     mPercent = 0; 
     mTime = " "; 
     mSpeed = 0; 
     mData = " "; 
    } 

    private volatile DownloadState mDownloadState = DownloadState.NOT_STARTED; 
    private final String mFilename; 
    private volatile Integer mProgress; 
    private final Integer mFileSize; 
    private volatile ProgressBar mProgressBar; 
    private volatile Integer mPercent; 
    private volatile String mTime; 
    private volatile Integer mSpeed; 
    private volatile String mData; 

    public ProgressBar getProgressBar() { 
     return mProgressBar; 
    } 

    public void setProgressBar(ProgressBar mProgressBar) { 
     this.mProgressBar = mProgressBar; 
    } 

    public Integer getProgress() { 
     return mProgress; 
    } 

    public void setProgress(Integer mProgress) { 
     this.mProgress = mProgress; 
    } 

    public String getFilename() { 
     return mFilename; 
    } 

    public Integer getFileSize() { 
     return mFileSize; 
    } 

    public Integer getPercent() { 
     return mPercent; 
    } 

    public void setPercent(Integer mPercent) { 
     this.mPercent = mPercent; 
    } 

    public String getTime() { 
     return mTime; 
    } 

    public void setTime(String mTime) { 
     this.mTime = mTime; 
    } 

    public Integer getSpeed() { 
     return mSpeed; 
    } 

    public void setSpeed(Integer mSpeed) { 
     this.mSpeed = mSpeed; 
    } 

    public String getData() { 
     return mData; 
    } 

    public void setData(String mData) { 
     this.mData = mData; 
    } 

    public DownloadState getDownloadState() { 
     return mDownloadState; 
    } 

    public void setDownloadState(DownloadState mDownloadState) { 
     this.mDownloadState = mDownloadState; 
    } 
} 

DonwloadInfoArrayAdapter.java

public class DownloadInfoArrayAdapter extends ArrayAdapter<DownloadInfo> { 

    //private final Context context; 
    //private List<DownloadInfo> downloadinfo; 
    public DownloadInfoArrayAdapter(Context downloadingFragment,int id,List<DownloadInfo> objects) { 
     super(downloadingFragment, id, objects); 
     //this.context=downloadingFragment; 
     //this.downloadinfo=objects; 
     // TODO Auto-generated constructor stub 
    } 
    private static class ViewHolder { 
     TextView downloadFilename; 
     TextView speed; 
     TextView time; 
     TextView percent; 
     TextView data; 
     ProgressBar progressBar; 
     DownloadInfo info; 
     } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.d("downloadinfoarrayadapter", "lefutott"); 
     View row = convertView; 
     DownloadInfo info = getItem(position); 
     ViewHolder holder = new ViewHolder(); 
     if(null == row) { 

      LayoutInflater inflater = (LayoutInflater) 
        getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate (R.layout.row, parent,false); 

      holder.downloadFilename = (TextView) row.findViewById(R.id.downloadFileName); 
      holder.data = (TextView) row.findViewById(R.id.data); 
      holder.speed = (TextView) row.findViewById(R.id.speed); 
      holder.time = (TextView) row.findViewById(R.id.time); 
      holder.percent = (TextView) row.findViewById(R.id.percent); 
      holder.progressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar); 

      holder.info = info; 

      row.setTag(holder); 
      } else { 
      holder = (ViewHolder) row.getTag(); 

      holder.info.setProgressBar(null); 
      holder.info.setData(null); 
      holder.info.setSpeed(null); 
      holder.info.setTime(null); 
      holder.info.setPercent(null); 
      holder.info = info; 
      holder.info.setProgressBar(holder.progressBar); 
      holder.info.setData(holder.data.toString()); 
      holder.info.setSpeed(Integer.getInteger(holder.speed.toString())); 
      holder.info.setTime(holder.time.toString()); 
      holder.info.setPercent(Integer.getInteger(holder.percent.toString())); 

      } 
     holder.downloadFilename.setText(info.getFilename().toString()); 
     holder.data.setText(info.getData().toString()); 
     holder.speed.setText(info.getSpeed().toString()); 
     holder.time.setText(info.getTime().toString()); 
     holder.percent.setText(info.getPercent().toString()); 
     holder.progressBar.setProgress(info.getProgress()); 
     holder.progressBar.setMax(info.getFileSize()); 
     Log.d("downloadinfoarrayadapter", "ez lefutott"); 
     info.setProgressBar(holder.progressBar); 
     return row; 
    } 
} 

編輯:

的NPE問題解決了,多虧了黑帶,但應用程序運行到另一個問題。 更改的行:

View V = inflater.inflate(R.layout.list, null); 
ListView dlistView = (ListView) V.findViewById(R.id.dlist); 

之後:

01-11 10:53:48.285: E/AndroidRuntime(3714): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

我試圖找出其中的觀點是具體的孩子,但我不知道。

編輯: 這是新的堆棧:

01-11 16:12:51.056: E/AndroidRuntime(2973): FATAL EXCEPTION: main 
01-11 16:12:51.056: E/AndroidRuntime(2973): android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.content.res.Resources.getResourceName(Resources.java:1679) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at com.velsorange.filedownloader.MainActivity$DownloadingFragment.onCreateView(MainActivity.java:140) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1012) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.view.ViewPager.populate(ViewPager.java:881) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.View.measure(View.java:15513) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.View.measure(View.java:15513) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.View.measure(View.java:15513) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.View.measure(View.java:15513) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.Choreographer.doFrame(Choreographer.java:532) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.os.Handler.handleCallback(Handler.java:725) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.os.Looper.loop(Looper.java:137) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at android.app.ActivityThread.main(ActivityThread.java:5191) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 
01-11 16:12:51.056: E/AndroidRuntime(2973):  at dalvik.system.NativeStart.main(Native Method) 
+0

不要打電話與被設置爲TRUE;最後一個參數的'inflate'方法(使用'FALSE'),因爲這會將充氣的視圖添加到父級,並且這是「AdapterView」的子級所不允許的。它應該是'inflater.inflate(R.layout.row,parent,false);'。 – Luksprog

+0

感謝您的幫助,它的工作,如果我沒有得到另一個錯誤,什麼是真正的mistic。 01-11 16:12:51.056:E/AndroidRuntime(2973):android.content.res.Resources $ NotFoundException:無法找到資源ID#0xffffffff – velsorange

+0

我不能奇蹟般地猜測沒有看到(full )stacktrace與Logcat的例外。 – Luksprog

回答

0
ListView dlistView = (ListView) V.findViewById(android.R.id.list); 

應該

ListView dlistView = (ListView) V.findViewById(R.id.dList); 

,因爲你與@+id/dList

宣佈它這是一個很大的代碼去通過。下一次你應該只顯示相關的代碼片段。

View V = inflater.inflate(R.layout.list, container, false); 

這是錯誤的。改變它在:

View V = inflater.inflate(R.layout.list, null); 

也返回對象v,而不是名單

+0

對不起,很多代碼,謝謝你,你的代碼很好。 – velsorange

+0

它解決了你的問題嗎? – Blackbelt

+0

哦,我得到了另一個錯誤:01-11 09:35:58.481:E/AndroidRuntime(2641):java.lang.IllegalStateException:指定的子項已經有父項。您必須先調用子對象的父對象的removeView()。 我不知道孩子是哪個。也許V pr dlistView? V.getParent()爲空... – velsorange

相關問題