- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 Android 应用程序,它使用 Fragment 和 listView 在 HomeActivity 上生成两个列表,它还针对使用 NavigationViewer 进行菜单自定义。
主要 Activity :
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;
}
}
fragment 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);
}
}
fragment 2:
public class Fragment2 extends ListFragment {
int c=0;
String str[]= {"Wifi","Bluetooth"};
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 = new ApkAdapter(this,l ,c);
setListAdapter(ad);
return view;
}
public void onCreate(Bundle s) {
ListView l1;
l1=(ListView)getActivity().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 class ApkAdapter extends BaseAdapter {
List<PackageInfo> packageList;
List<String> s1;
Fragment1 context;
Fragment2 context1;
PackageManager packageManager;
Switch s;
int c;
String appName;
String str[];
int img[]={R.drawable.wifi,R.drawable.bluetooth};
public ApkAdapter(Fragment1 context, List<PackageInfo> packageList,
PackageManager packageManager, int c) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
this.c=c;
}
public ApkAdapter(Fragment2 context,List<String> s,int c)
{
super();
this.context1=context;
this.s1=s;
this.c=c;
}
private class ViewHolder {
TextView apkName;
ImageView i;
}
public int getCount() {
if (c == 1) {
return packageList.size();
}
else if(c==0)
{
return str.length;
}
else return 0;
}
public Object getItem(int position) {
if(c==1){
return packageList.get(position);
}
else
return position;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder,h1;
LayoutInflater inflater = context.getActivity().getLayoutInflater();
LayoutInflater inflater1 = context1.getActivity().getLayoutInflater();
ArrayList<String> ar = new ArrayList<String>();
if (convertView == null) {
if(c==0) {
convertView = inflater.inflate(R.layout.apklist_item, null);
}
else if(c==1)
{
convertView = inflater1.inflate(R.layout.apklist_item, null);
}
holder = new ViewHolder();
holder.apkName = (TextView) convertView.findViewById(R.id.appname);
holder.i=(ImageView)convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try{
if(c==0) {
int j = 0;
while (j < 2) {
int ser=(Integer) getItem(position);
appName=s1.get(ser);
Drawable dr = context.getResources().getDrawable(img[j]);
dr.setBounds(0,0,10,10);
resize(dr);
holder.apkName.setCompoundDrawablesWithIntrinsicBounds(img[j], 0, 0, 0);
holder.apkName.setCompoundDrawablePadding(3);
holder.apkName.setText("\n" + " " + appName);
j++;
}
}
else if(c==1) {
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 100, 100);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(25);
holder.apkName.setText("\n" + " " + appName);
}
holder.i.setScaleY(0.5f);
holder.i.setImageResource(R.drawable.images);
}
catch(Exception e) {
e.printStackTrace();
}
return convertView;
}
public Drawable resize(Drawable i)
{
Bitmap b=((BitmapDrawable)i).getBitmap();
Bitmap br=Bitmap.createScaledBitmap(b,10,10,false);
return new BitmapDrawable(Resources.getSystem(),br);
}
}
还有一个 AppData 类,它有 getPackageInfo()
和 setPackageInfo()
方法。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
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>
fragment_1.xml 和 fragment_2.xml 有 listView 和 TextView,前者使用 frameLayout,后者使用 linear。两者都有一个 id 标签设置为 android:idlist
apklist_item.xml : 它有一个 textview 和 ImageView。由于字数限制无法放置他们的代码。我想这不会有任何区别。
我在 logcat 中收到以下异常,但不知道如何解决它们:
FATAL EXCEPTION: main
Process: com.example.sardwal.applock, PID: 21919
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sardwal.applock/com.example.sardwal.applock.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.sardwal.applock.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.sardwal.applock.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
除此之外,最近当我在我的联想设备上运行相同的代码时,我在 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
我陷入了这些错误,却不知道如何解决它们。谢谢
最佳答案
应用程序崩溃,因为您正在 Fragments 的 onCreate 方法中查找您的列表。你应该在 fragment 中使用这样的东西:
@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);}
这是在 Fragment 的 onCreateView 方法之后调用的,因此您的 Fragment 布局将被设置,您将不会寻找空的 ListView。
关于java - InflateException 和 IllegalStateException,同时将 fragment 用于自定义列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38584315/
您好,所以在我们的项目中,我们使用的是书法库,它没有任何问题,但现在在我添加的一项 Activity 中,它不断崩溃并出现以下堆栈跟踪: 01-23 02:25:32.178 3654-3654/?
I published my application to the Play Store as open beta. I don't get any error when I run it wi
我因为工作室提示 Dialog 而苦苦挣扎已弃用, 所以我又去阅读文档,似乎 Toast也被弃用了。 所以我尝试从 Toast 迁移至Snackbar ,但目前失败...我 我加了 implement
每当我更改屏幕方向时,我的应用程序都会崩溃并出现以下异常。我相信这与 ImageViews 导致模拟器内存不足有关,但我无法理解堆栈跟踪。 在 ProfileActivity.java(我想允许改
InflateException 在使用 glide 加载大量图像时发生。 我做了一些研究并意识到 InflateException 可能不是我的 android 应用程序崩溃的真正原因,真正的问题应
我有更多的 Activity ,我没有任何问题,但是当我开始这个 Activity (Segunda)时它坏了在此 Activity 中,我尝试使用 customView 进行缩放和绘制,但我找不到问
我正在尝试创建一个自定义 View - 手指绘画,但我遇到了这个错误,我不知道如何修复它。谁能告诉我哪里出了问题? 日志: 08-04 08:34:01.171: E/AndroidRuntime(1
我的应用程序出现此错误 Error inflating class android.support.design.widget.FloatingActionButton 我在互联网上搜索错误,我尝试了
我有 recyclerview 项目,我正在尝试编辑它们的外观。我尝试向 recyclerview 的每个项目添加一个 float 操作按钮,但它引发了此错误: android.view.Inflat
我正在尝试与我的 fragment 进行通信,但代码与我不一致并给了我错误 E/AndroidRuntime: FATAL EXCEPTION: main Process
我对应用程序开发仍然很陌生,所以我正在努力创建一个跨越 5 页(我猜是 fragment )的基本音板。在 Android Studio 中创建项目时,我从默认的“选项卡式 Activity ”选项为
我已经做了一些尝试来修复这个错误。由于我不明白,所以我没有其他解决方法。 这是我的 XML -->
所以,我在 android.view.InflateException 方面遇到了很多麻烦,我不完全确定为什么。当我尝试加载 Google map fragment 时会发生这种情况。 对于此错误的任
我遇到了一个错误的痛苦。我有一个 SherlockFragmentActivity 和至少两个 SherlockFragment。两者都使用 onCreateView 来扩充 xml 文件。然而,Lo
启动具有 fragment 的 Activity 时,我不断收到该错误。 这是我的代码的概述: 主页 Activity 从启动 屏幕 Activity 开始 home.xml(每个 fragment
我使用 RingtonePreference 并希望使用默认通知声音作为默认首选项值。为此,我遵循了以下建议:RingtonePreference Default value 可悲的是,实际上我得到了
我的 xml 文件的第 59 行: 错误发生是因为 ?attr/属性。如果我将它替换为#FF00000 或其他内容,一切正常。这种崩溃并不经常发生。 07-28 19:16:46.686
我正在尝试设置一个虚拟测试以开始我的 android 项目。但是我无法让我的应用程序与 Espresso 一起运行。未经测试,该应用程序运行良好。 这是回溯: Running tests Test r
我在实现Jake Wharton's ViewPagerIndicator的时候发现了一些问题.这些是 Logcat 上的内容: 06-19 15:32:02.661: E/AndroidRuntim
我刚刚通过这个 setup 设置了 Robolectric 测试当我测试我的应用程序时,它给我一个错误。 我已将问题缩小到我的自定义 ActionBar 布局 中的 ProgressSpinner。当
我是一名优秀的程序员,十分优秀!