Android Floating Action Button not being recognized - java

When I look at my code in the editor, nothing seems to be a problem. However, when I run the code on my phone and try to go from one activity to another which features a floating action button, the floating action button cannot find the view by Id despite the ID being exactly the same in both java and XML.
My Java:
package com.test.app.app2_test1;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class TermsOfUse extends AppCompatActivity {
FloatingActionButton mFloatingActionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.terms_of_use);
mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab);
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.app.app2_test1.TermsOfUse"
android:background="#color/backdrop">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="44dp"
android:orientation="vertical">
</ScrollView>
<View
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#color/colorPrimary"
android:elevation="4dp"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/terms_back_fab"
android:layout_gravity="bottom"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="16dp"
android:src="#drawable/ic_arrow_back"
app:pressedTranslationZ="12dp"/>
</android.support.design.widget.CoordinatorLayout>
The message that I get in my console is the following:
Attempt to invoke virtual method 'android.view.View android.support.design.widget.FloatingActionButton.findViewById(int)' on a null object reference
Any help would be appreciated.

You are treating a view as a child of its own!
Replace:
mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab);
With:
mFloatingActionButton = (FloatingActionButton)findViewById(R.id.terms_back_fab);

Related

Creating subText with visible/invisible method across TextViews

I have created two textViews that the first one is the main title and the second one is the subtext of the title. So I want to make the subtexts visibility to depend on clicking the main title.
It looks so easy as saying butI tried a lot of methods and MainActivity.java crashes all the time.
On the second step I want a different main title under the first one and the subtext above can toggle between them.
Maybe there is an easier widget instead of textView but I couldn't find any.
Please help.
Thanks.
Ps: The strings work well, there is no problem with them.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="100dp"
android:clickable="true"
android:focusable="true"
android:text="#string/text1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.062" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text11"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textSize="15sp"
android:layout_alignTop="#id/textView"
android:layout_marginTop="35dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintVertical_bias="0.083"
android:visibility="invisible"
tools:visibility="visible" />
</RelativeLayout>
MainActivity.java
package com.example.Notes;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MainActivity);
}
}
Edit: adding to java this code is the solution:
TextView title = findViewById(R.id.textView);
TextView subText = findViewById(R.id.textView2);
title.setOnClickListener(v -> {
if (subText.getVisibility() == View.VISIBLE)
subText.setVisibility(View.INVISIBLE);
else
subText.setVisibility(View.VISIBLE);
});
To change the visibility of the subText by clicking the title textview one could do:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MainActivity);
TextView title = findViewById(R.id.textView);
TextView subText = findViewById(R.id.textView2);
title.setOnClickListener(v -> {
subText.setVisibility(View.VISIBLE); //OR View.GONE OR View.INVISIBLE
});
}
}
Thanks, I just modified this part and it became what I exatly want:
//subText.setVisibility(View.VISIBLE); //OR View.GONE OR View.INVISIBLE
if (subText.getVisibility() == View.VISIBLE)
subText.setVisibility(View.INVISIBLE);
else
subText.setVisibility(View.VISIBLE);

java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.support.design.widget.FloatingActionButton

It is not a clean and rebuild issue AT ALL! Make module will not
resolve this issue either. This is the only
FlotingActionButton I'm using in my code. One solution said this only
occurs with an anchor parameter but as you can see I am not using one
so I dont know what to make of it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/activity_main"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/coverpic"
tools:context="com.android.nohiccupsbeta.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/effects_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="top|end"
android:layout_marginTop="400dp"
android:layout_marginRight="15dp"
android:src="#drawable/ic_book"
app:backgroundTint="#color/primary_color" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/take_test"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:backgroundTint="#color/primary_color"
android:src="#drawable/ic_handcuffs" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
This is the JAVA code. I tried importing the Coordinator Layout but
that is not possible. I also tried declaring the fab var differently
just to get the same error :
package com.android.nohiccupsbeta;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, pageone.class));
}
});
FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);
fab2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, BacTest.class));
}
});
}
}
Your second FloatingActionButton does not have an id, and the one you are trying to cast into a FAB is the CoordinatorLayout.
So, you should set an id in your layout for your fabs like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/activity_main"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/coverpic">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/effects_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="top|end"
android:layout_marginTop="400dp"
android:layout_marginRight="15dp"
android:src="#drawable/ic_book"
app:backgroundTint="#color/primary_color" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/take_test"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:backgroundTint="#color/primary_color"
android:src="#drawable/ic_handcuffs" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
and your fabs to match the new ids like this
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);

Android Java programming error while switching screens

As mentioned in title, I face an error when I compile and try to run the following program in my phone.
Code:
(File mainActivity.java)
package com.game.myapp;
import android.app.*;
import android.os.*;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void VillageOnClick() {
Intent intent = new Intent(MainActivity.this, VillageView.class);
startActivity(intent);
}
}
(File VillageView.java)
package com.game.myapp;
import android.app.*;
import android.os.*;
public class VillageView extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.village_view);
}
}
(File main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"
android:layout_gravity="bottom">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="village"
android:onClick="VillageOnClick"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="research"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="battle"/>
</LinearLayout>
</LinearLayout>
(File village_view.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_margin="10dp">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"/>
</LinearLayout>
</LinearLayout>
Note:I write, compile, install and run the app on my android phone only using an app.
Note2: both the xml files are located in res/layout and most probable are not the cause of the error.
Note3: I do not understand Intent. Knowledge about the same is always appreciated.
Thanks in advance.
PS. Suggestions and change in format of code is always accepted happily.
Madworks.
you need to add the View in the VillageOnClick
public void VillageOnClick(View v) {
Intent intent = new Intent(MainActivity.this, VillageView.class);
startActivity(intent);
}

Opening one screen from another

I am trying to open one screen from another in an Android App. I copied the outline of the following code from a tutorial online and then tried to substitute my own screens. When I try to run the following code, I get a "Force Close" when I click the button on the first screen (the second screen never displays). Can someone tell me what I am doing wrong? There are four files: ScreenTestActivity.java, main.xml, screen2.java and screen2.xml.
ScreenTestActivity.java
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ScreenTestActivity extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.btnClick);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(ScreenTestActivity.this, screen2.class);
startActivity(i);
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open New Screen"
/>
</LinearLayout>
screen2.java:
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.os.Bundle;
public class screen2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
}
}
screen2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the New Screen"
/>
</LinearLayout>
<activity android:name="screen2"></activity>
<activity android:name="ScreenTestActivity"></activity>
put this 2 line of code in your androidmanifest.xml file

embed MapView into Activity

I am trying to embed MapView into FrameLayout of an activity for example ,
UserActivity layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="#+id/map_frame" android:layout_gravity="center_horizontal" android:padding="0.0dip" android:clipChildren="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4.0dip">
<ImageView android:id="#+id/map_img" android:layout_gravity="center" android:layout_width="294.0dip" android:layout_height="136.0dip" android:src="#drawable/place_map_frame" />
</FrameLayout>
</LinearLayout>
Here , inside FrameLayout i want to display MapView when ever the list item gets clicked
for some reason its not possible to directly put inside framelayout so just because of that i created another activity which extends MapActivity
<com.google.android.maps.MapView
android:layout_gravity="center"
android:id="#+id/mapview"
android:clickable="true"
android:layout_width="294.0dip"
android:layout_height="136.0dip"
android:layout_centerInParent="true"
android:apiKey="#string/google_maps_key" />
UserMap layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView android:layout_gravity="center" android:id="#+id/mapview" android:clickable="true" android:layout_width="294.0dip" android:layout_height="136.0dip" android:layout_centerInParent="true" android:apiKey="#string/google_maps_key" />
</LinearLayout>
Here is the code for both the activities
import com.google.android.maps.MapActivity;
import android.app.Activity;
import android.os.Bundle;
public class UserMap extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_map);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class UserActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_activity);
}
}
Now , i want to embed a UserMap into UserActivity's FrameLayout and UserActivity will be called when a list item get clicked.
Remember to include the Google API instead of the single Android API.
You also need to include the following line in your Manifest, under the <application> tag:
<uses-library android:name="com.google.android.maps" />

Categories

Resources