i am trying to start a new activity when a user click a imageView...
the imageView is on CardView.
I have added the Intent to the .java but still the app crashes..
here is my code
package com.example.rishav.thisiscarsearch20;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MarutiSuzukiScrollingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_maruti_suzuki_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void onBaleno (View view){
Intent baleno = new Intent(this, LoginActivity.class);
startActivity(baleno);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:background="#drawable/onmarutiselectcars"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.rishav.thisiscarsearch20.MarutiSuzukiScrollingActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:paddingBottom="8dp"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:text="Maruti Baleno"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="normal|bold" />
<ImageView
android:id="#+id/baleno01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/balenointro"
android:onClick="onBaleno" />
<TextView
android:id="#+id/textview01"
android:layout_width="273dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Price : 5.3 - 8.7 Lakh"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
when i run this on emulator i get the following error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rishav.thisiscarsearch20, PID: 2407
java.lang.IllegalStateException: Could not find method please(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'baleno01'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
public void onBaleno (View view){
Intent baleno = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(baleno);
}
Hope it helps!
bro i was also getting the same problem, so i made another activity and used the intent to visit the new activity and it works for that one so i just copies the content to new activity and i notice while coping from the old activity that import (toolbar) was showing not being used so i didn't copied that and content related to that and it is working..
Related
I am still pretty new to fragments and I am sure I do not understand things correctly. But I hope I can learn from this now.
I follow this guide for connecting my xml with the fragments: https://developer.android.com/guide/fragments/create. But when starting the app I get an error that says that some parts are not compatible.
Here are my files:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
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"
android:id="#+id/main"
android:orientation="horizontal"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/brand"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/item"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:srcCompat="#drawable/gibson_1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/price"
android:gravity="right"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_marginRight="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to cart"
android:drawableLeft="#drawable/drawablestart"
android:id="#+id/addCart"
/>
</LinearLayout>
</FrameLayout>
</androidx.fragment.app.FragmentContainerView>
MainActive.java
package at.hlpinkafeld.android.musicstore;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public MainActivity() {
super(R.layout.activity_main);
}
#Nullable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(R.id.main, ProductDetailFragment.class, null)
.commit();
}
}
}
fragment.java
package at.hlpinkafeld.android.musicstore;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.material.textview.MaterialTextView;
public class ProductDetailFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.activity_main,container,false);
System.out.println(view);
return view;
}
}
the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: at.hlpinkafeld.android.musicstore, PID: 13020
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.hlpinkafeld.android.musicstore/at.hlpinkafeld.android.musicstore.MainActivity}: android.view.InflateException: Binary XML file line #11 in at.hlpinkafeld.android.musicstore:layout/activity_main: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
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:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.view.InflateException: Binary XML file line #11 in at.hlpinkafeld.android.musicstore:layout/activity_main: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
Caused by: java.lang.IllegalStateException: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.kt:266)
at android.view.ViewGroup.addView(ViewGroup.java:5048)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1131)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at android.view.LayoutInflater.inflate(LayoutInflater.java:485)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:710)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:356)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
at at.hlpinkafeld.android.musicstore.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
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:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
For help and an explanation why it didn't work like that, would be nice
I think you might want to try loading a simple fragment into an activity.
Try this code. It's working for you.
For more info: click
Note: Always create a new object like this to load fragments new ProductDetailFragment() , and use setContentView(); inside of onCreate().
1. First of all, create the perfect activity to load a fragment.
MainActivity.class
public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(R.id.main, new ProductDetailFragment(), null)
.commit();
}
}
activity_main.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=".MainActivity"
android:id="#+id/main"
android:orientation="horizontal"
>
</RelativeLayout>
2. create fragment ProductDetailFragment.class
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class ProductDetailFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_product_details,container,false);
System.out.println(view);
return view;
}
}
3. make diffrent layout for fragment fragment_product_details.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/brand" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/item"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:srcCompat="#drawable/gibson_1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="#string/price" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:inputType="phone" />
<Button
android:id="#+id/addCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/drawablestart"
android:text="Add to cart" />
</LinearLayout>
</FrameLayout>
You misunderstood the actual code in the link :- https://developer.android.com/guide/fragments/create
You put the fragment xml code inside your fragment container view which is inside your main activity.
You just need to declare the fragment container view defined below.
The fragment container view is main container which contains your fragment like below in your main activity.
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="<provide your fragment class here from your code it is ProductDetailFragment>" />
Then create the fragment xml file in layout directory named fragment_product_detail:-
Then put your fragment xml code in it. like below.
fragment_product_detail.xml :-
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/brand"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/item"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:srcCompat="#drawable/gibson_1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/price"
android:gravity="right"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_marginRight="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to cart"
android:drawableLeft="#drawable/drawablestart"
android:id="#+id/addCart"
/>
</LinearLayout>
</FrameLayout>
Then in your fragment onCreateView method make sure to inflate the fragment layout like below :-
View view =inflater.inflate(R.layout.fragment_product_detail,container,false);
This will resolve your problem.
Let me know if you still face any issue.
This question already has answers here:
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
When i want to run app in my device i get this error in logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.allo/com.example.android.allo.WelcomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setAnimation(android.view.animation.Animation)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setAnimation(android.view.animation.Animation)' on a null object reference
at com.example.android.allo.WelcomeActivity.onCreate(WelcomeActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
WelcomActivty.java :
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
public class WelcomeActivity extends AppCompatActivity {
LinearLayout l1,l2;
Button btnsub;
Animation uptodown,downtoup;
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.l1);
LinearLayout linearLayout1=(LinearLayout) findViewById(R.id.l2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
btnsub = (Button)findViewById(R.id.buttoncom);
uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this,R.anim.downtoup);
l1.setAnimation(uptodown);
l2.setAnimation(downtoup);
}
protected void onStart(){
super.onStart();
linearLayout.startAnimation(uptodown);
linearLayout1.startAnimation(downtoup);
}
public void com(View view){
Intent intent = new Intent(this, Login.class);
Button buttoncom = (Button) findViewById(R.id.buttoncom);
startActivity(intent);
}
}
welcome.xml:
<LinearLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.allo.WelcomeActivity"
android:background="#drawable/background"
android:orientation="vertical">
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="120dp"
android:text="#string/bienvenue"
android:textColor="#color/lightorange"
android:textSize="60sp"
android:textStyle="bold" />
<TextView
android:textColor="#color/lightorangedark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/dans_quoi"
android:textAlignment="center"
android:textSize="20sp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/l2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="115dp"
android:background="#drawable/spaceullustration"
android:orientation="vertical">
<Button
android:id="#+id/buttoncom"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstyle"
android:text="#string/button_commencer" />
</LinearLayout>
activity_login.xml
<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"
android:background="#drawable/backgroudlogin"
tools:context=".Login">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="232dp"
android:background="#drawable/ic_email_button"
android:hint=" Email"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="159dp"
android:background="#drawable/ic_email_button"
android:hint=" password"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:background="#drawable/ic_sign_in"
android:text="Sign IN" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="create an account"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
Login.xml
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
i know, there is a lot of question in the forum with the same subject but I can not find the solution, so please do not mark the question as duplicated
You should do this way -
public class WelcomeActivity extends AppCompatActivity {
Button btnsub;
Animation uptodown,downtoup;
LinearLayout linearLayout;
LinearLayout linearLayout1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
linearLayout=(LinearLayout) findViewById(R.id.l1);
linearLayout1=(LinearLayout) findViewById(R.id.l2);
btnsub = (Button)findViewById(R.id.buttoncom);
uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this,R.anim.downtoup);
linearLayout.setAnimation(uptodown);
linearLayout1.setAnimation(downtoup);
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
$ error in androidmonitor
02-02 14:22:48.870 3269-3269/com.example.applincatio.t E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.applincatio.t, PID: 3269
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applincatio.t/com.example.applincatio.t.moddle}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebChromeClient(android.webkit.WebChromeClient)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebChromeClient(android.webkit.WebChromeClient)' on a null object reference
at com.example.applincatio.t.moddle.onCreate(moddle.java:20)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
$main activity.java
package com.example.applincatio.t;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import static com.example.applincatio.t.R.*;
import static com.example.applincatio.t.R.id.moddle1;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView moddle;
ImageButton outlook;
ImageButton contactus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
moddle = (ImageView) findViewById(moddle1);
ImageView moddle = (ImageView) findViewById(moddle1);
moddle.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case moddle1:
Intent imprintIntent = new Intent(MainActivity.this, moddle.class);
imprintIntent.putExtra("webivew", moddle1);
this.startActivity(imprintIntent);
break;
case id.outlook:
Intent contactIntent = new Intent(MainActivity.this, moddle.class);
// contactIntent.putExtra("webivewContact", outlook);
this.startActivity(contactIntent);
break;
case id.contactus:
Intent aboutIntent = new Intent(MainActivity.this, moddle.class);
//aboutIntent.putExtra("webivewAbout", contactus);
this.startActivity(aboutIntent);
break;
}
}
}
$moddle.java
package com.example.applincatio.t;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class moddle extends AppCompatActivity {
String url;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
Intent intent = this.getIntent();
if (intent != null) {
Bundle data = getIntent().getExtras();
if (data.containsKey("webivew")) { //i have changed this param to match the intent passed
url = data.getString("webivew");
}
if (data.containsKey("webivewContact")) {
url = data.getString("webivewContact");
}
if (data.containsKey("webivewAbout")) {
url = data.getString("webivewAbout");
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView wView, int progress) {
// webViewActivity.setTitle("Loading...");
// webViewActivity.requestWindowFeature(progress * 100);
if(progress == 100) {
// webViewActivity.setTitle(R.string.app_name);
}
}
});
webView.loadUrl(url);
}
}
}
$activitymain.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/activity_main"
android:layout_width="match_parent"
android:background="#drawable/p"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.example.applincatio.t.MainActivity"
android:weightSum="1">
<GridLayout
android:layout_width="match_parent"
android:rowCount="4"
android:columnCount="2"
android:layout_height="530dp"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:id="#+id/gridview"
android:padding="14dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/moddle1"
android:src="#drawable/moddle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MODDLE"
android:textColor="#fff"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="96dp"
android:layout_height="139dp"
android:id="#+id/outlook"
android:layout_gravity="center"
android:src="#drawable/ou"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OUTLOOK"
android:gravity="bottom"
android:textAlignment="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="122dp"
android:layout_height="141dp"
android:layout_gravity="center"
android:id="#+id/contactus"
android:src="#drawable/cont"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONTACT US"
android:textSize="20dp"
android:textColor="#ffff"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="123dp"
android:layout_height="128dp"
android:onClick="onClick"
android:layout_gravity="center"
android:src="#drawable/vi"
android:id="#+id/visitus" />
<TextView
android:layout_width="wrap_content"
android:layout_height="48dp"
android:text="VISIT US"
android:gravity="bottom"
android:textAlignment="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
</LinearLayout>
</GridLayout>
</LinearLayout>
when i run the app. it is running but when i click on any imagebutton the app keeps stopping. i cant understand the mistake i did .in image you can see my layout its running but imagebuttons are not working
please any one help me
thanks in advance
Try the below one,
package com.example.applincatio.t;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class moddle extends AppCompatActivity {
String url;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
Intent intent = this.getIntent();
if (intent != null) {
Bundle data = getIntent().getExtras();
if (data.containsKey("webview")) {
url = data.getString("webview");
}
if (data.containsKey("webivewContact")) {
url = data.getString("webivewContact");
}
if (data.containsKey("webivewAbout")) {
url = data.getString("webivewAbout");
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView wView, int progress) {
// webViewActivity.setTitle("Loading...");
// webViewActivity.requestWindowFeature(progress * 100);
if(progress == 100) {
// webViewActivity.setTitle(R.string.app_name);
}
}
});
url = "https://moodle.kluniversity.in/login/index.php";
webView.loadUrl(url);
}
}
}
Thanks!
I'm sure this question has been asked a few times; however, after searching for a while, all I find is "to declare the variable before calling the clicklistner" I've done that already. But still getting the same error. If someone can let me know why the clicklistener called on the getQuoteBotton is giving the following error.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
mainActivity
package com.example.george.radonquote;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import static com.example.george.radonquote.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
TextView titleTextView;
ImageView mainImageView;
Button getQuoteBotton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
titleTextView = (TextView) findViewById(R.id.main_title);
mainImageView = (ImageView) findViewById(R.id.main_image);
getQuoteBotton = (Button) findViewById(R.id.get_quote_btn);
getQuoteBotton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent quoteActivityIntent = new Intent(getApplicationContext(), QuotesActivity.class);
startActivity(quoteActivityIntent);
}
});
}
}
activity_main.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#154350"
tools:context="com.example.george.radonquote.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Random Quotes"
android:textColor="#fff"
android:textSize="27dp"
android:textAlignment="center"
android:id="#+id/main_title"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="#drawable/vector_socrates"
android:id="#+id/main_image"
android:layout_below="#+id/main_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="84dp" />
<Button
android:id="#+id/get_quote_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/main_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="get quote" />
</RelativeLayout>
I have searched for an answer that works for me but have not come across anything that helped.
My problem is that I am linking a TextView to another activity, however when clicked on throws a NullPointerException
This is the error:
01-18 07:03:41.882 14021-14035/com.j2fx.msc_driverlogin E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7d7650
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: FATAL EXCEPTION: main
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: Process: com.j2fx.msc_driverlogin, PID: 14021
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.j2fx.msc_driverlogin/com.j2fx.msc_driverlogin.RegisterNew}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
01-18 07:03:42.729 14021-14021/com.j2fx.msc_driverlogin E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
(quite a lot more stack trace but probably not relevant)
Here is the Login.class
package com.j2fx.msc_driverlogin;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends AppCompatActivity implements View.OnClickListener {
Button bLogin;
EditText etUsername, etPassword;
TextView tvRegisterLink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
bLogin = (Button) findViewById(R.id.bLogin);
tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);
bLogin.setOnClickListener(this);
tvRegisterLink.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bLogin:
// will add login code here
break;
case R.id.tvRegisterLink:
startActivity(new Intent(this, Register.class));
break;
}
}
}
Here is the Activity_Login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Driver ID"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="#+id/etUsername"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:layout_marginBottom="10dp"
android:id="#+id/etPassword"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/bLogin"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:text="Register new account"
android:id="#+id/tvRegisterLink"/>
</LinearLayout>
Here is the Register.class
package com.j2fx.msc_driverlogin;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Register extends AppCompatActivity implements View.OnClickListener {
Button bRegister;
EditText etName, etAddress, etDateOfBirth, etVehicleReg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etName = (EditText) findViewById(R.id.etName);
etAddress = (EditText) findViewById(R.id.etAddress);
etDateOfBirth = (EditText) findViewById(R.id.etDateOfBirth);
etVehicleReg = (EditText) findViewById(R.id.etVehicleReg);
bRegister.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRegister:
break;
}
}
}
And finaly the Activity_Register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="#+id/etName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="#+id/etAddress"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:layout_marginBottom="10dp"
android:id="#+id/etDateOfBirth"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vehicle Reg"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginBottom="10dp"
android:id="#+id/etVehicleReg"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:id="#+id/bRegister"/>
</LinearLayout>
I have checked thoroughly and cannot see where I have gone wrong, although I am new to this so possibly missing something trivial.
Some points:
The id tvRegisterLink is within the same view
I have initialised tvRegisterLink before setOnClickListener
I have declared tvRegisterLink before the method onCreate
There is no duplication of the tvRegisterLink ID
Any ideas or pointers in the right direction would be great !
Thanks.
You are never assigning bRegister in the Register.onCreate method before setting the click listener on it.
bRegister = (Button) findViewById(R.id.bRegister);
You are not instantiating Button bRegister; in class Register, why it is giving error while receiving the click event.
Take a look at the error and the Register Class
ava.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object referenc
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etName = (EditText) findViewById(R.id.etName);
etAddress = (EditText) findViewById(R.id.etAddress);
etDateOfBirth = (EditText) findViewById(R.id.etDateOfBirth);
etVehicleReg = (EditText) findViewById(R.id.etVehicleReg);
bRegister.setOnClickListener(this);
}
bRegister is not initialised.
just add
bRegister = (Button) findViewById(R.id.bRegister);