Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为自定义列表使用片段时的InflateException和IllegalStateException_Java_Android - Fatal编程技术网

Java 为自定义列表使用片段时的InflateException和IllegalStateException

Java 为自定义列表使用片段时的InflateException和IllegalStateException,java,android,Java,Android,我正在开发一个android应用程序,它使用Fragment和listView生成两个HomeActivity列表,并且它还为使用NavigationViewer进行菜单定制 主要活动: public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { PackageManager packageManage

我正在开发一个android应用程序,它使用Fragment和listView生成两个HomeActivity列表,并且它还为使用NavigationViewer进行菜单定制

主要活动:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener  {


    PackageManager packageManager;
    ListView apkList,l;
    ImageView i;
    int c=0,sys=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                Intent i1 = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(i1);
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        Fragment1 f1=new Fragment1();
        Fragment2 f2=new Fragment2();
        ft.replace(R.id.fragment_2,f2);
        ft.replace(R.id.fragment_1,f1);
        ft.commit();


    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify img parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.themes) {
            Intent i1=new Intent(MainActivity.this,SettingsActivity.class);
            startActivity(i1);

        } else if (id == R.id.settings) {
            Intent i1=new Intent(MainActivity.this,SettingsActivity.class);
            startActivity(i1);
        } else if (id == R.id.share) {

        } else if (id == R.id.send) {

        } else if (id == R.id.rating) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
片段1:

public class Fragment1 extends ListFragment {
 PackageManager pm;
    ListView apList;
    int c = 1;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public Fragment1() {

    }


    public static Fragment1 newInstance(String param1, String param2) {
        Fragment1 fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        apList=(ListView) getActivity().findViewById(R.id.listView);
        pm = getActivity().getPackageManager();
        List<PackageInfo> packageList = pm
                .getInstalledPackages(PackageManager.GET_PERMISSIONS);

        List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();

        for (PackageInfo pi : packageList) {
            boolean b = isSystemPackage(pi);
            if (!b) {
                packageList1.add(pi);
            }

        }

        apList.setAdapter(new ApkAdapter(this, packageList1, pm, c));

    }
    private boolean isSystemPackage(PackageInfo pkgInfo) {
        if((pkgInfo.applicationInfo.flags & (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) >0)
        {
            return true;
        }
        else{
            return true;
        }
 }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_1, container, false);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {

        void onFragmentInteraction(Uri uri);
    }
}
content_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/fragment_2"
        android:name="com.example.sardwal.applock.Fragment2"
        tools:layout="@layout/fragment_2" />
    <fragment
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/fragment_1"
        android:name="com.example.sardwal.applock.Fragment1"
        tools:layout="@layout/fragment_1"
        android:layout_marginTop="30dp" />
</LinearLayout>
除此之外,最近我在我的联想设备上运行相同的代码时,我在logcat错误上得到了以下错误:

 E/AppOnlineConfigurationUpdateTask: Get app configuration fail. Target host must not be null, or set in parameters. scheme=null, host=null, path=/reaper/server/appparams
                                                                   java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=/reaper/server/appparams
                                                                       at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:603)
                                                                       at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:299)
                                                                       at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:601)
                                                                       at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
                                                                       at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)
                                                                       at com.lenovo.lps.reaper.sdk.i.b.a(Unknown)
                                                                       at com.lenovo.lps.reaper.sdk.i.a.run(Unknown)
                                                                       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                       at java.lang.Thread.run(Thread.java:818)

E/ConfigurationUpdateTask: Same stuff IllegalStateException 

 E/SurfaceFlinger: strok layer name=none

 E/EventReportHandler: Same stuff IllegalStateException

 E/SurfaceFlinger: strok layer name=none

 E/WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 1540 num clients 10
 E/WifiTrafficPoller:  packet count Tx=1476881 Rx=1685658
 E/InputMethodManagerService: Ignoring updateSystemUiLocked due to an invalid token. uid:1000 token:null
 E/SwitchMobileDataTile: mobile data not ready or disabled
 E/InputDispatcher: channel 'f377d75  (server)' ~ Channel is unrecoverably broken and will be disposed!
 E/MountService: setDefaultPath new path=/storage/emulated/0
我陷入了这些错误中,不知道如何解决它们。
谢谢,应用程序崩溃了,因为您正在片段中的onCreate方法中查找列表。您应该在片段中使用类似的内容:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { ListView l1;
    l1=(ListView)view.findViewById(R.id.listView1);
    List<String> l=new ArrayList<>();
    l.add("Wifi");
    l.add("Bluetooth");
    ApkAdapter ad=new ApkAdapter(this,l,c);
    l1.setAdapter(ad);}
@覆盖
public void onViewCreated(视图,@Nullable Bundle savedInstanceState){ListView l1;
l1=(ListView)view.findViewById(R.id.listView1);
列表l=新的ArrayList();
l、 添加(“Wifi”);
l、 添加(“蓝牙”);
ApkAdapter ad=新的ApkAdapter(this,l,c);
l1.setAdapter(ad);}

这是在片段的onCreateView方法之后调用的,因此将设置片段的布局,并且您不会寻找空的ListView。

在您的情况下,为了工作,您必须执行以下操作: 片段1 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">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
接下来在onCreateView中,您应该有这样的内容:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle s) {
        View view = inflater.inflate(R.layout.fragment_2, vg, false);

        List<String> l = new ArrayList<>();
        l.add("Wifi");
        l.add("Bluetooth");
        ApkAdapter ad = //choose whichever constructor you want.
        setListAdapter(ad);
        return view;
    }
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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="vertical"
    android:weightSum="1">

    <fragment
        android:id="@+id/fragment_1"
        android:name="com.example.atstanescu.myapplication.Fragment1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        tools:layout="@layout/fragment_2" />

    <fragment
        android:id="@+id/fragment_2"
        android:name="com.example.atstanescu.myapplication.Fragment2"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="30dp"
        tools:layout="@layout/fragment_1" />
</LinearLayout>

One additional note, your adapter looks really buggy. I needed to reduce it a lot to see some issues. For example you check for c==0 and c==1 but what happens if you have 2, 3.
@覆盖
创建视图上的公共视图(布局、充气机、视图组vg、捆绑包s){
视图=充气机。充气(R.layout.fragment_2,vg,false);
列表l=新的ArrayList();
l、 添加(“Wifi”);
l、 添加(“蓝牙”);
ApkAdapter ad=//选择所需的构造函数。
setListAdapter(ad);
返回视图;
}
活动布局xml应该如下所示:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle s) {
        View view = inflater.inflate(R.layout.fragment_2, vg, false);

        List<String> l = new ArrayList<>();
        l.add("Wifi");
        l.add("Bluetooth");
        ApkAdapter ad = //choose whichever constructor you want.
        setListAdapter(ad);
        return view;
    }
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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="vertical"
    android:weightSum="1">

    <fragment
        android:id="@+id/fragment_1"
        android:name="com.example.atstanescu.myapplication.Fragment1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        tools:layout="@layout/fragment_2" />

    <fragment
        android:id="@+id/fragment_2"
        android:name="com.example.atstanescu.myapplication.Fragment2"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="30dp"
        tools:layout="@layout/fragment_1" />
</LinearLayout>

One additional note, your adapter looks really buggy. I needed to reduce it a lot to see some issues. For example you check for c==0 and c==1 but what happens if you have 2, 3.

另一个注意事项是,您的适配器看起来确实有问题。我需要减少很多,才能看到一些问题。例如,检查c==0和c==1,但如果有2,3会发生什么。

重新格式化了代码谢谢,我还没有检查c的情况,我想如果我一开始将其设置为1而不是增量,问题就会解决。是的,如果有帮助,请告诉我。在我的例子中,我可以看到一些虚拟数据。它仍然在主要活动:44i中给出了膨胀异常。e、 ,setContentView()片段1和2 xmls都应该有这个android:list,这是因为您使用了ListFragment扩展,也因为您没有使用FrameLayout,而是直接添加了片段。我编辑了我的答案。它已经解决了一个listView异常,但是RuntimeException:InflateException仍然没有解决。尽管Nullpointerexception操作系统已解决,但它仍然给出了该InflateException。。。。
   <?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">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment1 f1 = new Fragment1();
    Fragment2 f2 = new Fragment2();

    ft.replace(R.id.fragment_1, f1);
    ft.replace(R.id.fragment_2, f2);

    ft.commit();
}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle s) {
        View view = inflater.inflate(R.layout.fragment_2, vg, false);

        List<String> l = new ArrayList<>();
        l.add("Wifi");
        l.add("Bluetooth");
        ApkAdapter ad = //choose whichever constructor you want.
        setListAdapter(ad);
        return view;
    }
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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="vertical"
    android:weightSum="1">

    <fragment
        android:id="@+id/fragment_1"
        android:name="com.example.atstanescu.myapplication.Fragment1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        tools:layout="@layout/fragment_2" />

    <fragment
        android:id="@+id/fragment_2"
        android:name="com.example.atstanescu.myapplication.Fragment2"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="30dp"
        tools:layout="@layout/fragment_1" />
</LinearLayout>

One additional note, your adapter looks really buggy. I needed to reduce it a lot to see some issues. For example you check for c==0 and c==1 but what happens if you have 2, 3.