inflate exception widget recycleview - java

On the line below an error is thrown
setContentView(R.layout.activity_main);
android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.app.widget.RecycleView
What am I missing?
Code
MainActivity.java
package com.example.xxx.yyyy;
import android.database.sqlite.SQLiteException;
//import android.support.v7.app.AlertController;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity";
DBHandler dbHandler = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.app.widget.RecycleView
android:id="#+id/esp_menu"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.app.widget.RecycleView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Error in xml. RecycleView instead of RecyclerView (r letter at the end of Recycler is missing)

Related

Unable to change background color of a fragment

I am new to android and currently learning right now.
I faced a problem related to android-fragments, I write code and it did nothing! Just show the fragment color.
when I tried to remove the color of the fragment, it worked! I am unable to rectify this problem. Please help!
XML -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">
<Button
android:id="#+id/button"
android:layout_width="113dp"
android:layout_height="50dp"
android:text="Switch1"
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.945" />
<fragment
android:id="#+id/fragment"
android:name="com.example.fragments.Color_Purple"
android:layout_width="390dp"
android:layout_height="385dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.68"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.137" />
</androidx.constraintlayout.widget.ConstraintLayout>
XML - Fragment code
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".Color_Purple">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C403FA" />
</FrameLayout>
JAVA-mainActivity.java
package com.example.fragments;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b1;
Fragment f1;
Boolean flag=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
f1=new Color_Purple();
// Toast.makeText(getApplicationContext(),"Button pressed",Toast.LENGTH_SHORT).show();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
if(flag)
{
findViewById(R.id.fragment).setBackgroundColor(Color.GREEN);
// Toast.makeText(getApplicationContext(),"Button pressed Color GREEN",Toast.LENGTH_SHORT).show();
flag=false;
}
else
{
findViewById(R.id.fragment).setBackgroundColor(Color.RED);
//Toast.makeText(getApplicationContext(),"Button pressed Color RED",Toast.LENGTH_SHORT).show();
flag=true;
}
}
});
}
}
Java - fragment java code
package com.example.fragments;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Color_Purple extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_color__purple, container, false);
}
}
Note:
If i remove android:background from xml - fragment, this code works but not with color.
just change :
Boolean flag = true;
to
boolean flag = true;
and remove android:background="#C403FA" from your TextView :
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C403FA" //remove this line
/>

Facing problem in findView (Android Studio)

I'm new on Android Studio and I'm working on a project.
I'm facing this error
I've declared listview but don't know why it's not working.
CODE:
package com.example.animation;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class view_rooms extends AppCompatActivity {
ListView listView;
String mTitle[]={"Room1","Room2","Room3","Room4","Room5"};
String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"};
int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_rooms);
listView=findViewById(R.id.ListView);
}
findViewById() searches the view by id into the layout file. In this case into the layout: R.layout.activity_view_rooms. You need to define into this layout one ListView widget and assign one id to search with findViewById().
For example into the layout activity_view_rooms you need to include the view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
And later you can get the instance of view with finViewById() in this way:
package com.example.animation;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class view_rooms extends AppCompatActivity {
ListView listView;
String mTitle[]={"Room1","Room2","Room3","Room4","Room5"};
String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"};
int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_rooms);
listView=findViewById(R.id.list_view_id);
}
You need to add this line android:id="#+id/ListView" in your activity_view_rooms.xml
So the code will looks like this:
<ListView
android:id="#+id/ListView"
android:layout_width="match_parent"
android:layout_height="match_parent">

List not displaying in android studio

Main Activity layout
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:titleTextColor="#color/white"
android:background="#color/colorPrimary"
android:id="#+id/ToolbarMain"
tools:targetApi="honeycomb">
</android.support.v7.widget.Toolbar>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:dividerHeight="10dp"
android:divider="#null"
android:layout_below="#+id/ToolbarMain">
</ListView>
</RelativeLayout>
main activity.java
package com.example.admin.ttabledemo;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupIViews();
iniToolbar();
}
private void setupIViews(){
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.ToolbarMain);
listview = (ListView)findViewById(R.id.lvMain);
}
private void iniToolbar(){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("TIMETABLE APP");
}
I am trying to build a time table app. List is not displaying. Just the titlebar.
Although in design listview is visible. please help.
Nothing is showing on the screen. I can't figure out what is wrong.
This is my first attempt to build an app. I am a complete beginner.
Use setAdapter() like this way.
String data[] = new String[]{"Most Popular", "UpComing", "Top Rated"};
ListView listview = (ListView) findViewById(R.id.lvMain);
YourAdapterName cma = new YourAdapterName(this, android.R.layout.simple_list_item_1, data);
listview .setAdapter(cma);
You have to add something to show. Try the following code:
String items[]={"one","two","three","four","five"};
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listview.setAdapter(arrayAdapter);

My app is crashing because of the "addView": the error on it is: Method invocation may produce nullPointException

// *** See the error marked below ***
package com.example.android.miwok;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class NumbersActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ArrayList<String> words = new ArrayList<String>();
words.add(0,"One");
words.add(1, "Two");
words.add(2, "Three");
words.add(3,"Four");
words.add(4,"Five");
words.add(5,"Six");
words.add(6,"Seven");
words.add(7,"Eight");
words.add(8,"Nine");
words.add(9,"Ten");
LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
TextView wordsView = new TextView(this);
wordsView.setText(words.get(0));
// ***
// Here is the addView method that's not working:
// ***
rootView.addView(wordsView);
}
}
I don't know why this crash is happening. When I want to test my app, it crashes and says that it has stopped and when I remove the code from "calling the linear layout" to "after the "rootView.addView(wordsView);" "
I just tested your code:
Java code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> words = new ArrayList<String>();
words.add(0,"One");
words.add(1,"Two");
words.add(2,"Three");
words.add(3,"Four");
words.add(4,"Five");
words.add(5,"Six");
words.add(6,"Seven");
words.add(7,"Eight");
words.add(8,"Nine");
words.add(9,"Ten");
LinearLayout rootView = findViewById(R.id.layout);
TextView textView = new TextView(this);
textView.setText(words.get(0));
rootView.addView(textView);
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<LinearLayout
android:id="#+id/layout"
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</android.support.constraint.ConstraintLayout>
As you can see the code works fine for me. Also you should provide your XML.

android - list cannot be resolved or is not a field

I ve been searching for hours and I can't figure out what I'm doing wrong.
"list cannot be resolved or is not a field"
here is my search.java file:
package com.example.testxxx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Search extends ListActivity {
private TextView mTextView;
private ListView mListView;
private String query;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mTextView = (TextView) findViewById(R.id.text);
mListView = (ListView) findViewById(R.id.list);
}
}
and here is my search.xml layout:
<?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=".Search" >
<TextView
android:id="#+id/text"
android:textColor="?android:textColorPrimary"
android:background="#android:drawable/title_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp" />
</RelativeLayout>
I have noticed that if i change
android:id="#android:id/list"
to
android:id="#android:id/list"
then it will find it, but if i do so, i will have another error because I am extending listactivity that need the listview to have the ID set this way (android:id="#android:id/list")
Do you have a solution? Thank you!
Just replace
mListView = (ListView) findViewById(R.id.list);
with
mListView = (ListView) findViewById(android.R.id.list);
or simply you can call
mListView = getListView()
as you are extending ListActivity and your xml layout contains a ListView with id android.R.id.list
when you referencing to internal ID's (ID's may be drawable ID's, String ID's or any resource) then those ID's will not be generated in your R.java. you have to import those ID's from where you referenced.

Categories

Resources