DrawerLayout and NavigationView - attribute 'menu' not found - java

I'm trying to add a Navigation Menu to an existing project in Android Studio. However, while following the instructions on the official website, I added a <android.support.v4.widget.DrawerLayout ... tag around the rest of my view, and under the main ConstraintLayout that has the main activity layout, I put the sample Navigation Draw underneath that:
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/my_navigation_items" />
The last line causes some problems though when building:
Error:error: attribute 'com. ... :menu' not found.
I can't figure out why this is happening. The app part of app:menu is defined in the DrawerLayout tag as: xmlns:app="http://schemas.android.com/apk/res-auto"
This seems to also have the effect of giving me the error Cannot resolve symbol R in my MainActivity.java file.
Anyway, any help is very much appreciated!

Do you have the compile 'com.android.support:design:27.0.2' in your build.gradle file?
If not, try to add it.

Related

FragmentManager begin transaction without container id

fragmentManager.beginTransaction().replace(R.id.fragment_container_view,mapFragment).commit();
As we know that, this way we can load or replace Fragment
But
In this snippet we are passing fragment container id.
Is there any way
to load fragment without passing ID or pass FragmentContainerView instead of id
you can use navigation graph action to navigate between fragments with one line of code like that :
Navigation.findNavController(this).navigate(R.id.action_fragment1_to_fragment2)
provided that :
you have a navigation graph that looks like this
<navigation 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/nav_graph"
app:startDestination="#id/fragment_1">
<fragment
android:id="#+id/fragment_1"
android:name="Your.Package.Fragment_1"
tools:layout="#layout/fragment_1_layout">
<action
android:id="#+id/action_fragment1_to_fragment2"
app:destination="#id/fragment_2" />
</fragment>
<fragment
android:id="#+id/fragment_2"
android:name="Your.Package.Fragment_2"
tools:layout="#layout/fragment_2_layout">
</fragment>
</navigation>
and you've written the correct code to setup your activity with navigation components.
If you don't know what is navigation components I highly recommend taking the navigation lab on android developers where it should instruct you how to setup your project with it.
Learn Jetpack Navigation | Android Developers (Java)
for more information about navigation components and fragments follow these links and start reading Android Developers Tutorials.
Fragments| Android Developers
Navigation Components | Android Developers

How to fix this > attribute app:constraintBottom_toBottomOf not found

Android newbie here.
I am building and AR app and now I'm planning to add a User Interface. I am trying to add a constraint layout to app:layout_constraintBottom_toBottomOf="parent", however Im getting the error that this constraint was not found
These are my codes and error messages:
All the lines of code I add in my activity_ux.xml file that begin with "app"
e.g. app:layout_constraintBottom_toTopOf do not seem to work, I just showed you an example here with one of them just to make it easier to debug I think.
I tried searching online, one of the solutions was that I should install "ConstraintLayout for Android" and "Solver for ConstraintLayout" in my SDK Tools, but I already had this installed.
<FrameLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.google.ar.sceneform.Overlei.Overlei">
<fragment android:name="com.google.ar.sceneform.ux.ArFragment"
android:id="#+id/ux_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/gallery_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
>
</LinearLayout>
</FrameLayout>
I expected this to just work, I don't know where I can start looking if an in-built function(such as this) does not work.
Thanks for the suggestions guys. I have managed to deal with the error by adding this line
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
into my build.gradle(module.app) under dependencies
Again, thanks for everything
You should use app:layout_constraintBottom_toBottomOf only in ConstraintLayout. You can try to use inside of FrameLayout
then android:gravity = "bottom" in your LinearLayout.
Wrap you FrameLayout in a ConstraintLayout or change the FrameLayout to ConstraintLayout

Android Studio cannot find symbol variable

I installed Android Studio about two days ago, I also installed the most updated version of Java and JDK 1.8 (I think it was Java SE 8u101). I am able to edit code and I'm following a tutorial from the official Android Studio website. (Here: https://developer.android.com/training/basics/firstapp/building-ui.html)
However, upon hitting run I've gotten the two following errors:
Error:(24, 57) error: cannot find symbol variable activity_display_message
Code for this:
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
Error:(33, 25) error: cannot find symbol variable EXTRA_MESSAGE
Code for this:
intent.putExtra(EXTRA_MESSAGE, message);
What might I be doing wrong? The emulator pops up and I can get to other apps on the phone but I just can't get my app to load. Should I have installed Java SE 8u102 instead?
I also got a second error message:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.
Any help is appreciated.
Below is my code for the first xml file, "activity_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="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
Below is my code for "activity_display_message.xml"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.appno1.DisplayMessageActivity">
</RelativeLayout>
Add this to your MainActivity:
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
You might have missed this line, which can be found in the "Build an Intent" part.
Refer this for more, from the original page.
I think you might lose the last note in the tutorial.
so you just add android:id attribute to the activity_display_message.xml file and it will be ok!oh actually you should add into the layout element which is the valid location.
My advice is, try to clean the project and build it again. If you want to access newly added resources you need to build the project. Cleaning will help to remove all previously autogenerated resources from project and building will help to create them again + new ones.
You should add EXTRA_MESSAGE as static final, like:
private static final String EXTRA_MESSAGE = "message";
Regarding this error,
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
check that the file res > layout > activity_display_message.xml as the id specified as android:id="#+id/activity_display_message"

Fragment Confusion

The Android developer documentation for fragments says this:
"Note: When you add a fragment to an activity layout by defining the
fragment in the layout XML file, you cannot remove the fragment at
runtime. If you plan to swap your fragments in and out during user
interaction, you must add the fragment to the activity when the
activity first starts, as shown in the next lesson."
And partly because of this I got into the habit of always using the fragment manager to add/remove fragments to/from my user interface (even if I did not want to "hot swap" them at runtime). I am 100% certain that when I tried to remove/replace "hard wired" XML fragments at runtime my app crashed with an exception.
I haven't really worked with XML fragments for months, but today, on a lark, I decided to play around and I find that I am able to swap XML fragments for other fragments and...it works? I can't find anything online that discusses a recent change in this behavior. It just works.
My layout code is here:
<?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:id="#+id/fl_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="mobappdev.demo.myapplication.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/frag"
android:name="mobappdev.demo.myapplication.BlankFragment"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/click_me"
android:onClick="clickMe"/>
</LinearLayout>
And here is the code that replaces the fragment:
public void clickMe(View view) {
FragmentManager manager = getSupportFragmentManager();
BlankFragment newFrag = BlankFragment.newInstance();
Fragment oldFrag = manager.findFragmentById(R.id.frag);
Log.i("TESTING", "old frag is null: " + (oldFrag == null));
manager.beginTransaction()
.replace(R.id.fl_frag, newFrag)
.commit();
}
It works without any problems. I've tried variations (putting the XML fragment in a FrameLayout for example) and it all seems to work just fine. I even tried variations such as remove/add and just remove and it all works without a problem.
So what am I missing?
Instead of replacing the fragment itself, you're adding a new fragment on top of that one. It's just a small issue with IDs, but you're replacing using the FragmentManager on the container (LinearLayout) instead of doing it on the fragment itself (R.id.fl_frag instead of R.id.frag)

Attempting to Implement a Thread for a Game Loop

I am a novice learning Android development and the next step I must take is to be capable to understanding threads to be able to implement a game loop.
I have taken knowledge from google and StackOverflow and snippets from examples to come up with a lump of code that seems to not be working:
MainActivity.java
GameSetup.java
GameView.java
activity_main.xml
game_setup.xml
MainActivity.java is a simply title screen with a play button. This seems to work fine and will link to the next activity. However, I am getting a crash whenever I attempt to load up the next activity. My guestimations based on the error lead me to believe that threads have issues with RelativeActivity, but I'm not so sure on what the issue may be exactly, I am rather confused.
Here is the error I am getting thrown, I am having issues trying to decipher the issue with it.
Thanks.
R.id.layout is a RelativeLayout but you are trying to cast it to GameView, that's causing your crash.
Edit:
To add your GameView to the RelativeLayout you can do
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.your.package.GameView
android:id="#+id/my_game_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
</RelativeLayout>
and retrive it with
GameView myGameView = (GameView) findViewById(R.id.my_game_view);
Hmm, from the code you have written so far, I can explain the reason for the ClassCastException.
You are declaring a RelativeLayout in both XML files.
Both RelativeLayouts use the same android:id "#+id/layout"
In GameView.java you try to find a view by specifying the id
m_game = (GameView) findViewById(R.id.layout);
IMO you are retrieving one of the RelativeLayouts and then try to cast it to a GameView. As this is not possible, Java throws the exception.

Categories