Reputation: 679
I am new to coding in Android Studio and have been trying to make an application for school. I have made a navigation drawer that launches a few new activities. Right now, it only launches two activities which are the settings activity (which has nothing in it yet) and my basketball tutorial home page activity. Whenever I launch the settings activity, the activity shows up and the application does not crash. However, when I click on my basketball tutorial home page activity, the app crashes for some reason. This activity contains the beginning of almost like a youtube like structure, where once you click on the buttons, it sends you to the video. I also have a splash screen that launches my main activity. Please help in anyway you can. It was not letting me add the imports for the java file onto this forum, so I have let them out, as well as the public class....extends....as it was not working. The whole file also comes up with no errors.
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>
Main Activity 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>
Main Activity 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;
}
});
}
Basketball Tutorial Home Page 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>
Basketball Tutorial 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);
}
Navigation Drawer Menu:
<?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>
Here is the log of the app:
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)
Please try to solve the problem quickly, as the project is due in about a week. Thanks in advance!
Upvotes: 0
Views: 3842
Reputation: 67239
In your Android Manifest you declare an Activity named basketball_tutorial_home_page
here:
<activity
android:name=".basketball_tutorial_home_page"
android:label="Basketball Tutorials"
android:parentActivityName=".MainActivity">
</activity>
While you didn't specify what the name of the Basketball Tutorial's Activity class actually is, I'm going to assume that you followed standards and that your Activity class name is UpperCamelCase (something like BasketballTutorial
), whereas your layout XML file is snake_case (something like basketball_tutorial_home_page.xml
).
If this is the case, then the problem is that you are not using the right name in your manifest. The android:name
attribute needs to refer to the Java class, not the layout XML file. Thus this attribute should look more like android:name=".BasketballTutorial"
.
Additionally, it looks like you have some odd attributes in your basketball tutorial XML, which may be causing the error your are seeing in your logcat.
android:theme="@style/TextAppearance.AppCompat.Body2"
, which is overriding the entire theme for the buttons. You only want to override the text appearance using android:textAppearance="@style/TextAppearance.AppCompat.Body2"
. Overriding the whole theme could result in some odd attributesandroid:inputType
isn't needed for Buttons- since you can't input text into a button, setting an input type won't do anything.android:layout_weight
, you should set either android:layout_height
or android:layout_width
to 0dp
to specify whether you want the width or the height to stretch.Upvotes: 2
Reputation: 599
"Skipped 50 frames! The application may be doing too much work on its main thread." Try running in a new thread.
Thread thread = new Thread() {
@Override
public void run() {
try {
while(true) {
sleep(1000);
handler.post(this);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
Upvotes: 0