Fatal Exception null object reference in Android studio - android-studio

I'm working on an app where on a button click, the app will take the user from main activity to new activity. I've got everything coded and I am just trying to link up the activities now but I'm getting fatal errors which I cannot really understand. The app crashes when I try the button in testing and in Debug I get the error which I have pasted below. I've also pasted my code for the button and the XML for main activity. Can anybody help me make some sense of this error please? Also, if I have left any information out please let me know. Thanks.
Note: In the code for my button, the button I get the error with is the newactivity one.
Error:
2021-06-15 21:15:39.489 8340-8340/com.project.playshow E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.playshow, PID: 8340
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.playshow/com.fproject.playshow.newactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.project.playshow.newctivity.onCreate(newactivity.kt:97)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
Code used in MainActivity for Button (newactivityopen is the one I'm having issues with)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var dialogbutton = findViewById<Button>(R.id.dialogbtn)
dialogbutton.setOnClickListener{
openDialogue()
}
var newactivityopen = findViewById<Button>(R.id.button_newactivity)
newactivityopen.setOnClickListener{
var intent = Intent(this, newactivity::class.java)
startActivity(intent);
}
}
XML File
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/dialogbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"/>
<Button
android:id="#+id/button_newactivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="569dp"
android:text="NewActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

I´m not familiar with Kotlin (only java), but is it possible that you have setted the onClickListener to a Button, which doesn´t exist?
Try replacing these code snippets:
sdkButton.setOnClickListener with dialogbutton.setOnClickListener
button_newactivity.setOnClickListener with newactivityopen.setOnClickListener

Related

Unresolved reference by Android Studio 2020.3.1

I've used the Fullscreen Activity to create a project in Android Studio 2020.3.1. On buld of the app, I get "Unresolved reference: LastChange". The 3 lines pointed to (in FullscreenActivity.kt) are:
override fun onConfigurationChanged(newConfig: Configuration) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
LastChange.setText("Landscape")}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
LastChange.setText("Portrait")}
else {
LastChange.setText("Unrecognized")}
super.onConfigurationChanged(newConfig)
}
The TextView (in activity_xml) is coded as:
<TextView
android:id="#+id/LastChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/LastChangeText"
android:textSize="24sp" />
LastChangeText (in strings.xml) is defined as:
<string name="LastChangeText">N/A</string>
Although your textView has "LastChange" id, that's not the way you call it from activity.
At "onCreate" method, after super.onCreate(), you'll need:
setContentView(R.layout.<YOUR_LAYOUT_NAME>);
val lastChangeTextView = (TextView) findViewById(R.id.LastChange);
And, after that, you can call methods on "lastChangeTextView".
lastChangeTextView.setText("Landscape")
Strongly recommend taking a look at Documentation:
https://developer.android.com/guide/topics/ui/declaring-layout

Android Studio Bottom Navigation caused error

I dont know how to solve it i try too much but if i make a bottom navigation my app unfortunately stopped working after splash screen plz help me
When i remove bottom navigation app works perfect but when i add bottom navigation it not works but i need to add bottom navigation How can i resolve this error
my xml code
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_nav"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_menu"
tools:layout_editor_absoluteX="1dp"
android:background="#color/white"
app:itemIconTint="#00695C"
app:itemTextColor="#00695C"
tools:layout_editor_absoluteY="49dp"
/>
My java file code
bottomNavigationView.findViewById(R.id.bottom_nav);
bottomNavigationView.setOnNavigationItemSelectedListener(onNav);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new HomeFragment()).commit();
`private BottomNavigationView.OnNavigationItemSelectedListener onNav=new BottomNavigationView.OnNavigationItemSelectedListener()` {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selected = null;
switch (item.getItemId()){
case R.id.nav_home:
selected = new HomeFragment();
break;
case R.id.nav_chat:
selected = new ChatFragment();
break;
case R.id.nav_discussion:
selected = new DiscussionFragment();
break;
case R.id.nav_notification:
selected = new NotificationFragment();
break;
case R.id.nav_profile:
selected = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, selected).commit();
return true;
}
};
MENU
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/nav_home"
android:title="#string/home"
app:showAsAction="always"
android:icon="#drawable/ic_home"
/>
<item android:id="#+id/nav_chat"
android:title="#string/chat"
app:showAsAction="always"
android:icon="#drawable/ic_inbox"/>
<item
android:id="#+id/nav_discussion"
android:title="Discussion"
app:showAsAction="always"
android:icon="#drawable/ic_discussion"/>
<item
android:id="#+id/nav_notification"
android:title="#string/notification"
app:showAsAction="always"
android:icon="#drawable/ic_notification"/>
<item
android:id="#+id/nav_profile"
android:title="#string/profile"
app:showAsAction="always"
android:icon="#drawable/ic_profile"/>
</menu>
Error Shown in logcat
07-05 01:24:26.822 5495-5514/com.example.appname E/ActivityThread: Failed to find provider
info for com.google.android.gms.chimera
07-05 01:24:26.823 5495-5514/com.example.appname E/DynamiteModule: Failed to load IDynamiteLoader from GmsCore: Application package com.google.android.gms not found
07-05 01:24:26.830 5495-5514/com.example.appname E/DynamiteModule: Failed to load IDynamiteLoader from GmsCore: Application package com.google.android.gms not found
07-05 01:24:27.591 5495-5515/com.example.appname E/ActivityThread: Failed to find provider info for com.google.android.gms.phenotype
07-05 01:24:32.340 5495-5495/com.example.appname E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appname, PID: 5495
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paragonssoft.community/com.paragonssoft.community.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.material.bottomnavigation.BottomNavigationView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.material.bottomnavigation.BottomNavigationView.findViewById(int)' on a null object reference
at com.example.appname.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

How to address "Cannot access class" error when binding to a class in a subpackage?

Starting with Android 3.2.1, binding to classes defined in sub-packages (e.g. Sub.Thing in com.example) results in an error:
Cannot access class 'Sub.Thing'. Check your module classpath for missing or conflicting dependencies
What's the cause of this errror? How can it be fixed?
Sample Code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
>
<data>
<import type="com.example.Sub.Thing"/>
<variable name="data" type="Thing"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:text="#{data.name}"/>
<TextView
android:id="#+id/value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:text="#{data.value}"/>
</LinearLayout>
</layout>
Thing.kt:
package com.example.Sub
data class Thing(
var name:String,
var value:String
)
MainActivity.kt:
package com.example
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.Sub.Thing
import com.example.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
val activityMain:ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
activityMain.data = Thing("bam", "bug-AWWK!")
}
}
By convention (and for good reason), only class names are supposed to start uppercase. It appears newer versions of the compiler assume (enforce?) this convention (uppercase letters later in the name are accepted, though this may cause other issues).
The solution is simple: rename the subpackage to use lowercase, which can be done by right-clicking it in the Android project view and clicking Refactor → Rename, or by clicking it and opening the Refactor → Rename menu item.

Android EditText get nothing

it's my EditText.
I run the app don't type anthing
when I sout gettext().toString
I got really nothing
not null or "" why?
<EditText
android:id="#+id/ed_account"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg_edittext"
android:hint="#string/employeeHint1"
android:inputType="number"
android:text=""
android:paddingLeft="10dp" android:paddingStart="10dp"
/>
this is for someone to type some info for me to do something
I use hint, I don't want type anything to over the hint
and I have to make sure user to type something.
Try to modify this line
android:text="some text...."
your question is not clear, am sorry but please improve your english. And without the java code, am not sure i got your problem right, but try using this to check if the EditText is null:
EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return;
}

Is it possible to have two alignments to the application name of an android app?

I am tyring to achieve two alignments in the title of an android app. The title according to my requirement needs to have a 'name' which is left aligned and a 'version number' which is right aligned.
So far what I have tried doing is to test whether the label attribute of the application tag of the AndroidManifest.xml files accepts html, but it does not seem to :(. Also from some of the other similar questions I came to know that the style of the title could be changed using a custom theme. But this does not answer my question:
"Can we have two different alignments (say left and right), or for that case two different themes, to the parts of the application name?"
P.S: A screenshot of the desired application title is attached below.
You can create custom title bar ..
here is sample code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.i_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
final TextView titleLeft = (TextView) findViewById(R.id.title_left);
final TextView titleRight = (TextView) findViewById(R.id.title_right);
titleLeft.setText("Checker");
titleRight.setText("Version 5.44");
}
i_title.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/title_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="app name" />
<TextView
android:id="#+id/title_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="app version" />
</RelativeLayout>

Resources