0

我是新手 Android 开发人员。我的项目目标是制作一个下载器应用程序。我决定用片段来做到这一点。我有一个 NPE,但我不知道出了什么问题。这些是我的代码:

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>

列表.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>

行.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;
        }
    }

}

下载info.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)
4

1 回答 1

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 对象而不是列表

于 2013-01-11T08:20:51.913 回答