Why my ActivityMainBinding is showing error? - android

I was using binding object. My button id in the main activity xml file was 'button' but in the java class its showing red color on button in binding.button
package com.example.admybrand_git_api;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.progressindicator.BaseProgressIndicator;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(inflater, container, false);
binding.button.setOnClickListener(view->{
Intent intent = new Intent(this,Git_Id_searchpage.class)
});
return binding.getroot();
}}

You didn't add necessary information so let's guess.
Check if:
you added this code to build.gradle
buildFeatures {
viewBinding true
dataBinding true
}
added necessary import in your Activity
it has the same name as your XML layout (not Activity class)
You don't need
setContentView(R.layout.activity_main);
if you use ViewBinding

if you are using data binding then be ensure you have implemented it correctly.
your layout should start with <layout> and also end with </layout>.
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:context=".activities.MainActivity">
<!--your activity code design -->
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
and you don't need to setContentView and infalteView both for single layout bind your layout with DataBindinngUtils.
MainActivity.class
public class MainActivity extends BaseActivity {
ActivityMainBinding activityMainBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.button.setOnClickListener(view-> {
Intent intent = new Intent(this,Git_Id_searchpage.class)
});
}

Related

Android Espresso Fragment Root ID Not Found

I just ran into this issue where I'm trying to test if a fragment is displayed, but for some reason I can't test based on the fragment's root ID. Here's the code:
MainActivity.java:
package com.gregspitz.simplefragmenttesttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.gregspitz.simplefragmenttesttest.MainActivity">
<fragment
android:id="#+id/main_fragment_holder"
android:name="com.gregspitz.simplefragmenttesttest.BasicFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
BasicFragment.java:
package com.gregspitz.simplefragmenttesttest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BasicFragment extends Fragment {
public BasicFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_basic, container, false);
}
}
fragment_basic.xml:
<LinearLayout 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:orientation="vertical"
android:id="#+id/basic_fragment"
tools:context="com.gregspitz.simplefragmenttesttest.BasicFragment">
<TextView
android:id="#+id/simple_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment"/>
</LinearLayout>
MainActivityTest.java:
package com.gregspitz.simplefragmenttesttest;
import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.*;
/**
* Created by gregs on 2/6/2018.
*/
public class MainActivityTest {
#Rule
public ActivityTestRule<MainActivity> mActivityTestRule =
new ActivityTestRule<MainActivity>(MainActivity.class);
#Test
public void mainActivity_basicFragmentIsDisplayedAtStart() {
onView(withId(R.id.basic_fragment)).check(matches(isDisplayed()));
}
#Test
public void mainActivity_basicFragmentInnerIdIsFound() {
onView(withId(R.id.simple_text)).check(matches(isDisplayed()));
}
}
The second test works where I'm looking for an ID within the layout, but the first test doesn't find the layout's root ID. I just don't understand why that doesn't work and what should I do instead? Thanks for the help.
When the fragment gets inflated on the view, the Android framework replaces the root ID of the fragment's layout to the ID of the fragment itself.
The LinearLayout will have the ID changed to the ID of the fragment.
#Test
public void mainActivity_basicFragmentIsDisplayedAtStart() {
onView(withId(R.id.main_fragment_holder)).check(matches(isDisplayed()));
}

getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);

pardon me if this is a stupid error. there are two fragments in my activity layout (xml) and in order to let them communicate i am trying to get the instance of one fragment inside my main activity. i have done it earlier but this time it throws erro (required videolisting fragment but found fragment) i am not sure what i have given wrong. might be just a silly mistake. can you please assist. Thanks in advance.
below is code snippet:
public class VideoListingActivity extends ActionBarActivity implements FilterFragment.OnFilterItemSelectedListener{
public VideoListFragment videoListFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_listing);
videoListFragment =(VideoListFragment) getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);
}
below is the layout:
<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"
tools:context="my activity context">
<fragment
android:layout_width="match_parent"
android:layout_height="#dimen/filter_fragment_height"
android:name="com.text1.text2.text3.pkg.FilterFragment"
android:id="#+id/frg_video_listing_filter_fragment"
></fragment>
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.text1.text2.text3.pkg.VideoListFragment"
android:id="#+id/frg_video_listing_video_listing_fragment"
></fragment>
</RelativeLayout>
Below is fragment class code
package com.rrdtech.vidyavaan.android.Fragments;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.rrdtech.vidyavaan.android.R;
public class VideoListFragment extends ListFragment {
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.video_list_fragment,container,false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
}
}
Below is stack trace
Error:(22, 91) error: inconvertible types
required: VideoListFragment
found: Fragment
Maybe you need to add the class attribute in XML.
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.text1.text2.text3.pkg.VideoListFragment"
class="com.text1.text2.text3.pkg.VideoListFragment"
android:id="#+id/frg_video_listing_video_listing_fragment"/>
You Need to know
You are using support library for Fragment using
getSupportFragmentManager(), In this case you should always use support package classes.
VideoListFragment extending android.app.ListFragment; instead of android.support.v4.app.ListFragment; (this thing causes the error). And make sure fragment layout has ListView with "#android:id/list" id.

Open an activity from a fragment (crashes)

I've been trying to open an activity from a fragment using the OnClick element in a TableRow but when I run the application it closes when trying to open the activity.
This is the Java code file:
package com.hello.turidf;
import com.hello.turidf.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabsIndexM001Help extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.m_001_help, container, false);
return rootView;
}
public void open_m001_map(View view) {
Intent openmap = new Intent(getActivity(),M001MapActivity.class);
startActivity(openmap);
}
}
This is the XML code file (fragment):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
.......>
<TableLayout
.......>
<TabelRow
android:onClick="open_m001_map"
....>
........
</TableRow>
</TableLayout>
</ScrollBiew>
In 'M001MapActivity' activity has not yet been modified, has the code created by default.
android:onClick execute the method passed like parameter in Activity and not in Fragment. If you want execute this method in fragment, remove this line and set OnClickListener programatically on fragment.

Android SDK error: Trying instantiate a class that is not a fragment

I am hardly trying to create a simple application with a top menu and a changeable view below (by pressing the buttons in the menu fragment we change the view of the fragment below).
So, I have 2 fragments inside the main view but when trying to run the application in the emulator I get an error like:
Cause by android.app (bla bla bla, piece of crap Eclipse doesn't even allow copying the errors):
Trying to instantiate a class com.example.android.topmenu that is not a fragment
So, these are my XML layouts:
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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/menuFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.example.android.topmenu" >
</fragment>
<fragment
android:id="#+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.android.bottomcontent" >
</fragment>
</LinearLayout>
topmenu.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:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
bottom_content.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:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#+string/content_text" />
</LinearLayout>
and these are the classes for the main activity and the fragments
main_activity
package com.example.android;
import com.example.android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class OLife extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// The activity is being created
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.main);
}
#Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
super.onDestroy();
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
}
topmenu
package com.example.android;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OLifeMenu extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.topmenu, container, false);
return view;
}
}
bottomcontent
package com.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OLifeMain extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_content, container, false);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
You should be using FragmentActivity instead of Activity that is because you are using support Fragments and multiple Fragments in your activity main
Edit
You can now use the Appcompat support library and extends AppCompatActivity to support toolbar and fragment for lower api.
In my case it turned out, I was doing stuff in onCreate in the wrong order:
setContentView(R.layout.activity_qr_code_scan);
super.onCreate(savedInstanceState);
instead of
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_code_scan);
As you are using fragments in your layout and I suggest you to extend your class from fragment or fragment activity.

Android Fragment Error

I have a very basic application in which I am trying to create ListFragment on the MainActivity.
I keep on getting the following runtime error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.proto1.listfragment/com.proto1.listfragment.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
I have two classes.
The first is MainActivity.java as shown below:
package com.proto1.listfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The second is the class relating to the listfragment SectionsList as shown below:
package com.proto1.listfragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class SectionsList extends ListFragment {
//Create an array to hold the sections
String sections [] = new String []{
"Starters", "Mains", "Sides", "Desserts", "Drinks"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//Create array adapter to store list of menu sections
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,sections);
//Set list adapter of fragment
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
I then have one XML layout file called activity_main as shown below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation = "horizontal"
android:layout_height="fill_parent" >
<fragment
android:name = "com.proto1.listfragment.MainActivity"
android:id="#+id/sectionFrag"
android:layout_width = "wrap_content"
android:layout_height = "fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
tools:context=".MainActivity" />
I am unable to see why this error is being generated. Can someone please shed some light on the issue? Many thanks!
A FragmentActivity is not a Fragment in itself. But rather, it is a way to easily interact with Fragments. The Fragment you call in your xml should be SectionList.
Edit following into your activity_main xml file
<fragment
android:name = "com.proto1.listfragment.SectionList"
android:id="#+id/sectionFrag"
android:layout_width = "wrap_content"
android:layout_height = "fill_parent"/>

Categories

Resources