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">
Related
I'm making an app, now i want to make a button with an image and some text on it.
But every time I click the button my app crashes, android studio does compile however.
This is how i my xml code to make the layout of the screen.
<?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">
<RelativeLayout
android:id="#+id/buttonStartTest"
style="#android:style/Widget.Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<ImageView... />
<TextView.../>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java code:
package com.example.user.ballbounceapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout StartTest = findViewById(R.id.buttonStartTest);
StartTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TestActivity();
}
});
}
public void TestActivity(){
Intent intent = new Intent(MainActivity.this,TestActivity.class);
startActivity(intent);
}
};
You may have forgot to declare your TestActivity inside your manifest.
More about activities in manifest here : https://developer.android.com/guide/topics/manifest/activity-element
So you will need something like this :
<activity android:name="TestActivity">
If the crash persists, the problem might be in your TestActivity 's OnCreate method.
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);
// *** 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.
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)
I am making an android launcher, and i need to know how i can load all the apps into a gridview. I want system stuff like calculator and all of that to show up as well. How can i do that? I have search for a long time now without any solution. Please help me and thanks very much!
Java code:
package com.mysoftware.mysoftwareos.launcher;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.LinearLayout;
public class AllAppsActivity extends Activity {
LinearLayout allappsLayout;
GridView allappsGridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allapps_screen);
//Import views
allappsLayout = (LinearLayout)findViewById(R.id.allappsLayout);
allappsGridView = (GridView)findViewById(R.id.allappsGridView);
//Setup animation for the main layout
Animation a = AnimationUtils.loadAnimation(this, R.anim.fadeout);
a.reset();
allappsLayout.clearAnimation();
allappsLayout.startAnimation(a);
//Load all apps into the apptray
}
}
My xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/allappsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper"
android:orientation="vertical" >
<GridView
android:id="#+id/allappsGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" >
</GridView>
</LinearLayout>
ADW.Launcher is an open source android launcher...
browse around: http://code.google.com/p/adw-launcher-android/