enter code here
在测试我的应用程序时,我在Android Studio-Consol中收到关注错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.buratinoapps.wallpaper, PID: 7700
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buratinoapps.wallpaper/com.example.buratinoapps.wallpaper.HomeActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #16: Error inflating class android.support.design.widget.TableLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2812)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2890)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1611)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:6633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #16: Error inflating class android.support.design.widget.TableLayout
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.TableLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.TableLayout" on path: DexPathList[[zip file "/data/app/com.example.buratinoapps.wallpaper-fPrKTt4RFBuXWYEc0AM1Zg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.buratinoapps.wallpaper-fPrKTt4RFBuXWYEc0AM1Zg==/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:610)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:999)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:999)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.buratinoapps.wallpaper.HomeActivity.onCreate(HomeActivity.java:29)
at android.app.Activity.performCreate(Activity.java:7084)
at android.app.Activity.performCreate(Activity.java:7075)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2765)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2890)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1611)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:6633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
MainActivity.java
package com.example.buratinoapps.wallpaper;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Splash Screen
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homeIntent);
finish();
}
},3000); // Sleep 3 seconds
}
}
HomeActivity.java
导入android.support.design.widget.TabLayout;
package com.example.buratinoapps.wallpaper;
公共类HomeActivity扩展了AppCompatActivity 实现NavigationView.OnNavigationItemSelectedListener {
ViewPager viewPager;
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("EDMT Wallpaper");
setSupportActionBar(toolbar);
viewPager = (ViewPager)findViewById(R.id.viewPager);
MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager(), this);
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@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.home, 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 a 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.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
适配器代码
MyFragmentAdapter.java
打包com.example.buratinoapps.wallpaper.Adapter;
公共类MyFragmentAdapter扩展了FragmentPagerAdapter {
private Context context;
public MyFragmentAdapter(FragmentManager fm,Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position) {
if (position == 0)
return CategoryFragment.getInstance();
else if (position == 1)
return DailyPopularFragment.getInstance();
else if (position == 2)
return RecentsFragment.getInstance();
else return null;
}
@Override
public int getCount() {
return 3;
}
// Ctrl+O
@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Category";
case 1:
return "Daily Popular";
case 2:
return "Recents";
}
return "";
}
}
片段代码
CategoryFragment.java
打包com.example.buratinoapps.wallpaper.Fragment;
公共类CategoryFragment扩展了片段{
private static CategoryFragment INSTANCE=null;
public CategoryFragment() {
// Required empty public constructor
}
public static CategoryFragment getInstance(){
if (INSTANCE == null)
INSTANCE = new CategoryFragment();
return INSTANCE;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_category, container, false);
}
}
DailyPopularFragment.java
打包com.example.buratinoapps.wallpaper.Fragment;
公共类DailyPopularFragment扩展了片段{
private static DailyPopularFragment INSTANCE=null;
public DailyPopularFragment() {
// Required empty public constructor
}
public static DailyPopularFragment getInstance(){
if (INSTANCE == null)
INSTANCE = new DailyPopularFragment();
return INSTANCE;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_daily_popular, container, false);
}
}
RecentsFragment.java
打包com.example.buratinoapps.wallpaper.Fragment;
公共类LatestsFragment扩展了片段{
private static RecentsFragment INSTANCE=null;
public RecentsFragment() {
// Required empty public constructor
}
public static RecentsFragment getInstance(){
if (INSTANCE == null)
INSTANCE = new RecentsFragment();
return INSTANCE;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_recents, container, false);
}
}
答案 0 :(得分:0)
提示在您的logcat的以下行中:
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.TableLayout"
确保将设计库包含在gradle文件中:
compile 'com.android.support:design:28.0.0'
如果目标sdk不是28,请使用适当的库号来匹配您应用的sdk。