I want to select the button element by ID, but when I run the program, I get the app keeps stopping error.
I ran the program in debug mode and the error Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference
Even when I click on R.id.button, the button element is displayed in the xml file.
mainActivityFile :
package com.example.app;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity6 extends AppCompatActivity {
private Button btn;
private TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
btn.findViewById(R.id.button);
txt.findViewById(R.id.txt);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txt.setText("This is a new text.");
}
});
// Toast.makeText(this, "str", Toast.LENGTH_LONG).show();
}
}
xml file :
<?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=".MainActivity6">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="25sp"
android:autoSizeMaxTextSize="100dp"
android:autoSizeStepGranularity="2sp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
btn.findViewById(R.id.button);
You're not supposed to use it this way because btn is null at this point.
You have to do the following:
btn = (Button)findViewById(R.id.button);
Related
I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible
This is activty_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=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<EditText
android:hint="ADD ITEMS"
android:layout_margin="10dp"
android:id="#+id/etItems"
android:textSize="24sp"
android:textColor="#android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
<Button
android:layout_margin="10dp"
android:id="#+id/btnAdd"
android:text="ADD"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvItems"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
</LinearLayout>
This is MainActivity.Java
package com.example.todolist_09082020;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> items;
Button btnAdd;
EditText etItems;
RecyclerView rvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItems = findViewById(R.id.etItems);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
final ItemAdapter adapter = new ItemAdapter(items);
rvItems.setLayoutManager(new LinearLayoutManager(this));
rvItems.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = etItems.getText().toString();
if(!text.equals("")){
items.add(text);
adapter.notifyDataSetChanged();
}
etItems.setText("");
}
});
}
}
I tried solutions from other posts related to this, but this could not be solved.
edit text shrinked
Change layout_height of EditText and its parent LinearLayout to wrap_content. It should solve your issue
Your problem is the linear layout holding the text and button height. There's several things you can do, but a minimal way to keep it from collapsing is to change
android:layout_height="0dp"
to
android:layout_height="wrap_content"
You'll probably also see nicer results if you additionally change the text and button heights to wrap_content and choose a fixed font size (24sp looked nice) for your button text.
I am simply trying to make night mode activity but when the background turns black the text is not visible on screen. Text Disappears when switched to Dark Mode. Any solutions?
I tried tv.setTextColor(Color.WHITE); is also not working when the screen is black.
Any help would be appreciated. Thanks in advance.
Here are the images:
Image 1
Image 2
Here is the 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="com.saipriyank.daynight.MainActivity">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_margin="50px"
android:gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/background_view"
android:background="#android:color/background_dark"
android:alpha="0"/>
<com.mahfa.dnswitch.DayNightSwitch
android:id="#+id/dayNight"
android:layout_width="80dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Here is the Java Code:
package com.saipriyank.daynight;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.mahfa.dnswitch.DayNightSwitch;
import com.mahfa.dnswitch.DayNightSwitchListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DayNightSwitch dayNight = (DayNightSwitch)findViewById(R.id.dayNight);
final View background_view = (View)findViewById(R.id.background_view);
final TextView tv = (TextView)findViewById(R.id.tv);
dayNight.setDuration(450);
dayNight.setListener(new DayNightSwitchListener() {
#Override
public void onSwitch(boolean isNight) {
if(isNight){
Toast.makeText(MainActivity.this, "Night Mode Enabled",Toast.LENGTH_SHORT).show();
tv.setAlpha(0f);
background_view.setAlpha(1f);
}
else {
background_view.setAlpha(0f);
}
}
});
}
}
You are making the TextView invisible by calling tv.setAlpha(0f).
If you want to set the text color white, you can use tv.setTextColor(Color.White);
This is the error I am getting
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bignerdranch.android.geoquiz, PID: 7499
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: **java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference**
This is my code:
package com.bignerdranch.android.geoquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
}
}
I have no clue why I'm getting this error. I thought I assigned the object reference well and I checked the punctuation to make sure.
This is my XML code if that helps with the buttons and the ids.
<?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="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation= "vertical"
tools:context="com.bignerdranch.android.geoquiz.QuizActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding = "24dp"
android:orientation="horizontal">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="false"/>
</LinearLayout>
</LinearLayout>
Ok so I wanted to try and check if the values were null or not and I put these two lines of code to check
System.out.println("mTrueButtonIsNull:" +(mTrueButton==null));
System.out.println("mFalseButtonIsNull:" +(mFalseButton==null));
and I got back true and I don't know why or what I have to do now.
5-27 18:11:10.266 20014-20014/com.bignerdranch.android.geoquiz I/System.out: mTrueButtonIsNull:true
05-27 18:11:10.266 20014-20014/com.bignerdranch.android.geoquiz I/System.out: mFalseButtonIsNull:true
It appears you have a problem with one of your Buttons. which is defined as null.
Make sure you have true_button and false_button inside the activity_quiz layout.
If there is no problem with your layout file, Clean and re-build your app.
Here is my MainActivity.java code :
package com.dg.buttontest3;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("dg","button was clicked");
}
}
And here is the activity_main.xml code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="18dp"
android:text="Button" />
</LinearLayout>
I am getting the following error while running this code on Eclipse ADT : button1 cannot be resolved or is not a field.
I don't understand what is wrong with my code. Please help.
You haven't posted your XML code, but as this code seems to be working properly:
button = (Button)findViewById(R.id.button1);
I'd guess that in your activity_main.xml you never create something called button1, or that you don't use the #+id/ prefix.
It should look something like:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"/>
Just realised something else, you need to change your LinearLayout opening tag so it has a >.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
That might cause the button to never properly be recognised.
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