I have a problem, when I call findViewById in a TextView throws NullPointerException, but not for the others elements. I've tried anything (change identifiers, call on OnCreateView, Clean and Build the project) and I don't Know what to do.
This is my Activity
public class RoutesActivity extends Activity {
ListView routesList;
EditText searchTxt;
TextView noneRouteTxt;
long uuid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routes);
noneRouteTxt = (TextView)this.findViewById(R.id.notroute_txt);
getControls();
Bundle extra = this.getIntent().getExtras();
getRoutesList(extra);
}
and this is my layout: activity_routes.xml
<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="com.geochild.app.com.geochild.app.activitities.RoutesActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="325dp"
android:background="#drawable/app_background_waves"
android:layout_above="#+id/linearLayout"
android:layout_below="#+id/header_layout">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/routes_listview"
android:visibility="invisible">
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_routes"
android:id="#+id/notroute_txt"
android:visibility="invisible"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Can you help me? Thanks
The NPE will be obviously thrown in the following line. But you should also present the first lines of the stack trace.
noneRouteTxt = (TextView)this.findViewById(R.id.notroute_txt);
And the NPE is thrown because, the layout the application is using when calling your onCreate() method is another one and not the 'activity_routes', where the id notroute_txt does not exist.
How many layouts are you using or do you have copied in your res folder?
Related
as my title says I'm here with a problem in Android studio (JAVA) with my app, how to call class as an Activity when class has extends LinearLayout?
My class is:
public class CustomCalendar extends LinearLayout {
My code where I try to call it:
Intent customCalendar = new Intent(MainActivity.this, CustomCalendarActivity.class);
startActivity(customCalendar);
I tried to make this:
public class CustomCalendarActivity extends AppCompatActivity {
CustomCalendar customCalendar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customCalendar = (CustomCalendar) findViewById(R.id.kalendoriaus_virsus);
customCalendar.SetUpCalendar();
}
}
My crash is this:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.kalendorius.CustomCalendar.SetUpCalendar()' on a null object reference
at com.example.kalendorius.MainActivity$2.onClick(MainActivity.java:75)
75 Line is:
startActivity(customCalendar);
How I setup my calendar:
My kalendorius.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:id="#+id/kalendoriaus_virsus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/soft_blue_green"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<ImageButton
android:id="#+id/atgalBtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#drawable/back" />
<TextView
android:id="#+id/dabartineData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_weight="3"
android:gravity="center"
android:text="Data"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
That's just a bit code where I use that kalendorius_virsus.
DONE. I figure out what was the problem. I just forgot to generate for database constructor:D Thank you for everyone who tried to help.
The problem is
customCalendar = (CustomCalendar) findViewById(R.id.kalendoriaus_virsus);
The groupView LinearLayout with id kalendorias_virsus in the kalendorias.xml is not a CustomCalendar so always return null.
You need to understand how to properly user a costum view
basic example CustomView ->
public class CustomView extends LinearLayout {
public CustomView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(getContext()).inflate(
R.layout.costum, null);
this.addView(view);
}
public void setCustomText(String text){
TextView textview = (TextView) findViewById(R.id.textViewId);
textview.setText(text);
}
}
layout custom.xml ->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/app_name"
android:gravity="center">
</TextView>
</LinearLayout>
activity.xml->
<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=".MainActivity">
<com.example.myapplication.CustomView
android:id="#+id/customViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity class->
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomView customView = findViewById(R.id.customViewId);
customView.setCustomText("Welcome ");
}
}
You have many errors in your code. We still need more information to determine why you are getting a NullPointerException (NPE) in the MainActivity.onClick() method. However, this will for sure crash:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customCalendar = (CustomCalendar) findViewById(R.id.kalendoriaus_virsus);
customCalendar.SetUpCalendar();
}
because customCalendar will be null because you have not called setContentView() before calling findBiewById().
You need to add
setContentView(R.layout.XXXXXX);
before you call findViewById(). The XXXXXX above must be the name of your layout XML file (without the .xml file extension).
I am an android beginner, just learning android static fragment concept, but after running my code getting app crash issue. Here is details:
MainActivity.java
package com.example.lalendrakumar.fragmentdemo;
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
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:weightSum="2">
<fragment
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
So anyone can solve my problem.
I'm assuming you want the two fragments to fill the page. "fill_parent" has been replaced now by "match_parent." However, if you use this, in theory, the two fragments will overlap one over the other.
To split them in the screen, each will have to be adjusted to height zero. This is how each fragment's dimensions should read
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
You don't mentioned which layout you are using, but i assume you are using LinearLayout. LinearLayout take orientation horizontal by default, and you gave height and width to match_parent(fill_parent is deprecated now and replaced by match_parent)
Please find more information about LinearLayout on this link
Replace your code with following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<fragment
android:id="#+id/fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
try commenting out these lines in fragment class (if they are present )
/* #Override
public void onAttach(Context context) {
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
*/
I am writing a Chess app in Android Studio 3.0, and I want a PopupWindow to appear when the user clicks btnPopUpWindow, a button on the Chessboard layout. Here' the chessboard layout XML:
activity_chessboard.xml:
<?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="me.androidchess.MainActivity"
android:id="#+id/chessboardlayout">
<Button
android:id="#+id/popUpButton"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="Pop Up Menu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp" />
</android.support.constraint.ConstraintLayout>
The XML layout above is loaded by Chessboard.java. Obviously, Chessboard would be the parent of the popup window, which is why I would want to use a RelativeLayout reference for the popup to see Chessboard.
Chessboard.java
public class Chessboard extends Activity {
Button btnPopUpWindow;
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chessboard);
btnPopUpWindow = findViewById(R.id.popUpButton); // This works
relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout); // <--- CRASHES HERE!!!
btnPopUpWindow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// https://www.youtube.com/watch?v=wxqgtEewdfo
gameTextArea.setText("Pop up Menu appears...");
}
});
}
Its that relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout); line which crashes the app.
I am assuming that findViewById() is failing to locate the ID chessboardlayout, but I can't figure out why. I can't find the R.id directory in Android Studio, but I notice that chessboardlayout appears in the drop-down options when I type out the command. Plus I see the Id when I walk through the debugger. So I assume the Id has been properly filed.
But when I follow the debugger, the findViewById() call returns null, which can't be good. Specifically, an "Unable to start activity" exception is thrown by performLaunchActivity() in ActivityThread.java No idea how that is happening.
I've read through the other findViewById() threads that pop up in StackOverview searches, and a lot of those issues seem to revolve around the item returned by findViewById() not matching the local variable Button x = findViewById(R.id.TextFieldHere); for example. But that doesn't seem to be the case here... unless I don't understand how a RelativeLayout is supposed to work here...? Any advice/insight is appreciated, thank you.
Your R.id.chessboardlayout is an android.support.constraint.ConstraintLayout so you cannot cast it to RelativeLayout or it will cause ClassCastException.
So if you still want to use RelativeLayout as root of your layout, change your activity_chessboard.xml to this:
activity_chessboard.xml
<?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="match_parent"
tools:context="me.androidchess.MainActivity"
android:id="#+id/chessboardlayout">
<Button
android:id="#+id/popUpButton"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="Pop Up Menu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp" />
</RelativeLayout>
Then in your activity use the findViewById() normally.
setContentView(R.layout.activity_chessboard);
btnPopUpWindow = findViewById(R.id.popUpButton);
relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout);
I just want to animate my button.
I am getting:
W/PropertyValuesHolder: Method setAlpha() with type int not found on target class class android.support.v7.widget.AppCompatButton
Mainactivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ObjectAnimator objectAnimator;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mListView= (ListView) findViewById(R.id.listView);
Button button= (Button) findViewById(R.id.button);
objectAnimator=ObjectAnimator.ofInt(button,"alpha",0,1).setDuration(1000);
objectAnimator.setTarget(button);
objectAnimator.start();
adapter myAdapter = new adapter(this);
AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(myAdapter);
animationAdapter.setAbsListView(mListView);
mListView.setAdapter(animationAdapter);
}
activity_main.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="autogenie.mp.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:id="#+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Why is it not animating?
What comes under property name parameter of method ofint() of objectanimator class?
The alpha property is of type float. You are trying to animate it with int values. The object animator tries to find a setAlpha(int) method in the View class and can't do it - hence the exception.
You either need to use ObjectAnimator.ofFloat() or the simpler button.animate().alpha() variant. I would personally recommend the second option - it's much cleaner and removed some boilerplate code.
I created layout file, consist of one linearlayout and two nested linearlayout inside the main one. When I using getParent method, it select the second nested linearlayout. My target was the first one nested linearlayout. So I gives the first nested linearlayout a ID called linear_top. Then I declared in the onCreateView, test and debug, I have no luck it shows that is null.
My target was AnimatedGifImageView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/music_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Love Story"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.music.flow.lib.AnimatedGifImageView
android:id="#+id/music_anim"
android:layout_width="match_parent"
android:layout_height="76dp"
android:scaleType="fitXY"
android:contentDescription="Animation"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RatingBar
android:id="#+id/music_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:numStars="4"
android:rating="3.5" />
<ImageView
android:id="#+id/btn_playmusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:src="#drawable/resume" />
</LinearLayout>
OnCreateView
linearTop = (LinearLayout) v.findViewById(R.id.linear_top);
AnimatedGifImageView animatedGifImageView =
(AnimatedGifImageView) linearTop.getChildAt(1); /* Null Exception */
Child views are indexed from 0. In other words, the first child view is 0, the second child view is 1... and so on.
Also, why don't you just get the the ImageView by it's ID?
AnimatedGifImageView musicAnim = (AnimatedGifImageView) findViewById(R.id.music_anim);
#Override
public View onCreateView(View v, String name, Context context,
AttributeSet attrs) {
LinearLayout linearTop = (LinearLayout) v.findViewById(R.id.linear_top);
ImageView animatedGifImageView = (ImageView) linearTop.getChildAt(1); // NullPointer
return super.onCreateView(v, name, context, attrs);
}
Does your code look like this? You are getting a NullPointerException because in onCreateView, the parameter View v may be null, as mentioned in the docs here. Are you using fragments? If not, then don't use onCreateView. Place your code instead in onCreate like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearTop = (LinearLayout) findViewById(R.id.linear_top);
ImageView animatedGifImageView = (ImageView) linearTop.getChildAt(1);
setContentView(R.layout.activity_nested_linearlayout);
}
also, as what Sound Conception mentioned, you could just try to get the view directly by using its id instead of manually getting it using an index.