我是Android Studio中的新手,并且一直在努力为学校制作应用程序。我做了一个导航抽屉,启动了一些新的活动。现在,它只启动两个活动,即设置活动(其中没有任何内容)和我的篮球教程主页活动。每当我启动设置活动时,活动都会显示,应用程序不会崩溃。但是,当我点击我的篮球教程主页活动时,应用程序因某些原因崩溃了。此活动包含几乎类似于youtube结构的开头,一旦您单击按钮,它就会将您发送到视频。我还有一个启动我的主要活动的启动画面。无论如何,请帮助你。它不是让我把java文件的导入添加到这个论坛上,所以我让它们出来,以及公共类......扩展....因为它不起作用。整个文件也没有错误。
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jehan.sportstutorials" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Sports Tutorials"
android:theme="@style/AppTheme" >
<activity android:name=".Splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="Sports Tutorials" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.default" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="Settings"
android:parentActivityName=".MainActivity" >
</activity>
<activity
android:name=".basketball_tutorial_home_page"
android:label="Basketball Tutorials"
android:parentActivityName=".MainActivity">
</activity>
</application>
</manifest>
主要活动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"
android:id="@+id/navigation_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_kitkat_height"
android:background="?colorPrimary"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="?colorPrimaryDark"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Welcome To Sports Tutorials!"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/md_text"
android:singleLine="false"
android:padding="20dp"
android:gravity="center"
android:textStyle="bold"
android:textIsSelectable="false" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:text="Just Swipe Left To Get Started!"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/md_text"
android:singleLine="false"
android:padding="20dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="italic"
android:layout_marginBottom="30dp"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="@drawable/action_bar_color"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ToolbarTheme" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="@bool/fitsSystemWindows"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/navigation_drawer_menu"
app:theme="@style/NavigationViewTheme" />
</android.support.v4.widget.DrawerLayout>
主要活动Java:
DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBar actionBar;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
if (navigationView != null) {
setupNavigationDrawerContent(navigationView);
}
setupNavigationDrawerContent(navigationView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
textView = (TextView) findViewById(R.id.textView);
switch (menuItem.getItemId()) {
case R.id.item_navigation_drawer_basketball:
menuItem.setChecked(true);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
Intent i = new Intent(MainActivity.this, basketball_tutorial_home_page.class);
startActivity(i);
return true;
case R.id.item_navigation_drawer_starred:
menuItem.setChecked(true);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_navigation_drawer_sent_mail:
menuItem.setChecked(true);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_navigation_drawer_drafts:
menuItem.setChecked(true);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_navigation_drawer_settings:
menuItem.setChecked(true);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
Intent intent1 = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent1);
return true;
case R.id.item_navigation_drawer_help:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
return true;
}
});
}
篮球教程主页xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/md_divider">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="?colorPrimary"/>
<android.support.v7.widget.Toolbar
android:layout_marginTop="0dp"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@drawable/action_bar_color"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ToolbarTheme" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="509dp"
android:layout_gravity="bottom"
android:background="@color/md_divider"
android:padding="7dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:elevation="4dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton1"
android:scaleType="fitStart"
android:background="#00FF00"
android:layout_gravity="center"
android:elevation="2dp"
android:src="@drawable/icon_shooting_tutorial"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Basketball Shooting Tutorial"
android:id="@+id/button1"
android:layout_gravity="center"
android:padding="5dp"
android:layout_weight="1"
android:theme="@style/TextAppearance.AppCompat.Body2"
android:textSize="14sp"
android:gravity="start|bottom"
android:background="#00FF00"
android:inputType="text" />
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Length: 04:43"
android:id="@+id/button2"
android:layout_gravity="center"
android:layout_weight="2"
android:theme="@style/TextAppearance.AppCompat.Body1"
android:gravity="start"
android:textSize="12sp"
android:textStyle="italic"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:background="#00FF00"
android:inputType="time"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
篮球教程Java:
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basketball_tutorial_home_page);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TypedValue typedValueColorPrimaryDark = new TypedValue();
basketball_tutorial_home_page.this.getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValueColorPrimaryDark, true);
final int colorPrimaryDark = typedValueColorPrimaryDark.data;
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(colorPrimaryDark);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_basketball_tutorial_home_page, 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_bar) {
return true;
}
return super.onOptionsItemSelected(item);
}
导航抽屉菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:checked="true"
android:id="@+id/item_navigation_drawer_basketball"
android:icon="@drawable/basketball_drawer"
android:title="Basketball" />
<item
android:id="@+id/item_navigation_drawer_starred"
android:icon="@drawable/ic_action_toggle_star"
android:title="Starred" />
<item
android:id="@+id/item_navigation_drawer_sent_mail"
android:icon="@drawable/ic_action_content_send"
android:title="Sent mail" />
<item
android:id="@+id/item_navigation_drawer_drafts"
android:icon="@drawable/ic_action_content_drafts"
android:title="Drafts" />
</group>
<item android:title="More">
<menu>
<item
android:id="@+id/item_navigation_drawer_settings"
android:icon="@drawable/ic_action_action_settings"
android:title="Settings" />
<item
android:id="@+id/item_navigation_drawer_help"
android:icon="@drawable/ic_action_action_help"
android:title="Help" />
</menu>
</item>
</menu>
以下是该应用的日志:
07-13 12:30:34.823 1011-1018/com.jehan.sportstutorials W/art﹕ Suspending all threads took: 8.605ms
07-13 12:30:34.896 1011-1023/com.jehan.sportstutorials I/art﹕ Background sticky concurrent mark sweep GC freed 3067(319KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 1511KB/1511KB, paused 23.822ms total 134.618ms
07-13 12:30:34.922 1011-1023/com.jehan.sportstutorials W/art﹕ Suspending all threads took: 25.693ms
07-13 12:30:35.187 1011-1023/com.jehan.sportstutorials I/art﹕ Background partial concurrent mark sweep GC freed 349(33KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 1477KB/2MB, paused 3.349ms total 132.790ms
07-13 12:30:35.201 1011-1018/com.jehan.sportstutorials W/art﹕ Suspending all threads took: 5.092ms
07-13 12:30:38.036 1011-1011/com.jehan.sportstutorials I/Process﹕ Sending signal. PID: 1011 SIG: 9
07-13 12:30:52.636 1153-1173/com.jehan.sportstutorials D/OpenGLRenderer﹕ Render dirty regions requested: true
07-13 12:30:52.644 1153-1153/com.jehan.sportstutorials D/﹕ HostConnection::get() New Host Connection established 0xae0c9580, tid 1153
07-13 12:30:52.724 1153-1153/com.jehan.sportstutorials D/Atlas﹕ Validating map...
07-13 12:30:52.972 1153-1173/com.jehan.sportstutorials D/﹕ HostConnection::get() New Host Connection established 0xae0c9e70, tid 1173
07-13 12:30:52.983 1153-1173/com.jehan.sportstutorials I/OpenGLRenderer﹕ Initialized EGL, version 1.4
07-13 12:30:53.003 1153-1173/com.jehan.sportstutorials D/OpenGLRenderer﹕ Enabling debug mode 0
07-13 12:30:53.035 1153-1173/com.jehan.sportstutorials W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-13 12:30:53.035 1153-1173/com.jehan.sportstutorials W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c4c640, error=EGL_SUCCESS
07-13 12:30:53.630 1153-1172/com.jehan.sportstutorials I/System.out﹕ 888.0
07-13 12:30:54.639 1153-1172/com.jehan.sportstutorials I/System.out﹕ 1073.4697
07-13 12:30:55.660 1153-1172/com.jehan.sportstutorials I/System.out﹕ 1252.9235
07-13 12:30:56.680 1153-1172/com.jehan.sportstutorials I/System.out﹕ 1437.9651
07-13 12:30:57.704 1153-1153/com.jehan.sportstutorials I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
07-13 12:30:57.731 1153-1153/com.jehan.sportstutorials I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
07-13 12:30:57.935 1153-1173/com.jehan.sportstutorials W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-13 12:30:57.935 1153-1173/com.jehan.sportstutorials W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c4c660, error=EGL_SUCCESS
07-13 12:30:59.594 1153-1153/com.jehan.sportstutorials I/Choreographer﹕ Skipped 94 frames! The application may be doing too much work on its main thread.
07-13 12:31:03.214 1153-1153/com.jehan.sportstutorials I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
07-13 12:31:03.237 1153-1153/com.jehan.sportstutorials W/ResourceType﹕ Too many attribute references, stopped at: 0x01010099
07-13 12:31:03.238 1153-1153/com.jehan.sportstutorials W/ResourceType﹕ Too many attribute references, stopped at: 0x0101009a
07-13 12:31:03.238 1153-1153/com.jehan.sportstutorials W/ResourceType﹕ Too many attribute references, stopped at: 0x0101009b
07-13 12:31:03.255 1153-1153/com.jehan.sportstutorials D/AndroidRuntime﹕ Shutting down VM
07-13 12:31:03.259 1153-1153/com.jehan.sportstutorials E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.jehan.sportstutorials, PID: 1153
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jehan.sportstutorials/com.jehan.sportstutorials.BasketballTutorial}: android.view.InflateException: Binary XML file line #53: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #53: Error inflating class Button
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.jehan.sportstutorials.BasketballTutorial.onCreate(BasketballTutorial.java:18)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 24
at android.content.res.TypedArray.getColor(TypedArray.java:401)
at android.widget.TextView.<init>(TextView.java:692)
at android.widget.Button.<init>(Button.java:111)
at android.widget.Button.<init>(Button.java:107)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:63)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:59)
at android.support.v7.internal.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:101)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:802)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:832)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.jehan.sportstutorials.BasketballTutorial.onCreate(BasketballTutorial.java:18)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
请尽快解决问题,因为项目大约需要一周时间。提前谢谢!
答案 0 :(得分:2)
在Android Manifest中,您在此处声明名为basketball_tutorial_home_page
的活动:
<activity
android:name=".basketball_tutorial_home_page"
android:label="Basketball Tutorials"
android:parentActivityName=".MainActivity">
</activity>
虽然您没有指定Basketball Tutorial的Activity类的名称实际上是什么,但我将假设您遵循标准并且您的Activity类名称是UpperCamelCase(类似于BasketballTutorial
),而您的布局XML文件是snake_case(类似basketball_tutorial_home_page.xml
)。
如果是这种情况,那么问题是您没有在清单中使用正确的名称。 android:name
属性需要引用Java类,而不是布局XML文件。因此,此属性应该更像android:name=".BasketballTutorial"
。
此外,您的篮球教程XML中看起来有一些奇怪的属性,这可能会导致您在logcat中看到的错误。
android:theme="@style/TextAppearance.AppCompat.Body2"
,它会覆盖按钮的整个主题。您只想使用android:textAppearance="@style/TextAppearance.AppCompat.Body2"
覆盖文本外观。覆盖整个主题可能会产生一些奇怪的属性android:inputType
- 因为您无法在按钮中输入文本,因此设置输入类型将不会执行任何操作。android:layout_weight
时,您应该将android:layout_height
或android:layout_width
设置为0dp
,以指定您是想要拉伸宽度还是高度。