The Intent is working for some activity's, for some specific activity intent is crashing I have changed and tried it in different ways but for some activity intent is not working, app is getting built but after running on the phone it does not show any error during the debugging time ? I tried it in different way but it still crashes can anyone help me solve it?
MainActivity java
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button conee = (Button)findViewById(R.id.b1);
conee.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
});
Button cupp = (Button)findViewById(R.id.b2);
cupp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ko = new Intent(MainActivity.this,Main3Activity.class);
startActivity(ko);
}
});
}
}
Main3Activity java
package cheercreams.design;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Main3Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
}
MainActivity 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="cheercreams.design.MainActivity">
<Button
android:id="#+id/b1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_x="25dp"
android:layout_y="17dp"
android:text="Cone"
android:textSize="14dp" />
<Button
android:id="#+id/b2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b1"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:text="cup"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b3"
android:layout_toRightOf="#+id/b2"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:text="Butterfly box"
android:textSize="14dp"/>
<Button
android:id="#+id/b4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_below="#+id/b1"
android:text="family pack"
android:textSize="14dp" />
<Button
android:id="#+id/b5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b4"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b2"
android:text="kulfi"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b6"
android:layout_toRightOf="#+id/b5"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b3"
android:text="stick"
android:textSize="14dp"/>
<Button
android:id="#+id/b7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_below="#+id/b4"
android:text="sundae"
android:textSize="14dp" />
<Button
android:id="#+id/b8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b4"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b5"
android:text="special pack"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b9"
android:layout_toRightOf="#+id/b5"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b6"
android:text="cassette"
android:textSize="14dp"/>
</RelativeLayout>
Main3Activity 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="cheercreams.design.Main3Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/heading"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:id="#+id/t1"
android:layout_marginTop="20dp"/>
<ImageView
android:layout_width="240dp"
android:layout_height="200dp"
android:id="#+id/i1"
android:layout_below="#+id/t1"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:background="#drawable/bs"
android:layout_toRightOf="#+id/i2"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="45dp"
android:numStars="5"
android:id="#+id/r1"
android:rating="4.5"
android:contextClickable="true"
android:layout_marginLeft="60dp"
android:layout_below="#+id/i1"
/>
<TextView
android:layout_width="60dp"
android:layout_height="30dp"
android:text="Type :"
android:textSize="20dp"
android:id="#+id/t4"
android:layout_below="#+id/r1"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="60dp"
android:layout_height="30dp"
android:text="Price:"
android:textSize="20dp"
android:id="#+id/t5"
android:layout_below="#+id/t4"
android:layout_marginLeft="60dp"
android:layout_marginTop="25dp"
/>
<TextView
android:layout_width="40dp"
android:layout_height="30dp"
android:id="#+id/t6"
android:text="#string/Rs"
android:textSize="20dp"
android:layout_below="#+id/b1"
android:layout_toRightOf="#+id/t5"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/b1"
android:text="500ml"
android:layout_below="#+id/r1"
android:layout_toRightOf="#+id/t4"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/b2"
android:text="1000ml"
android:layout_below="#+id/r1"
android:layout_toRightOf="#+id/b1"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"/>
<ImageButton
android:layout_width="80dp"
android:layout_height="70dp"
android:id="#+id/i2"
android:layout_below="#+id/t1"
android:background="#drawable/bs"
android:layout_marginTop="60dp"
android:layout_marginLeft="2dp"
/>
<ImageButton
android:layout_width="80dp"
android:layout_height="70dp"
android:id="#+id/i3"
android:layout_marginTop="2dp"
android:layout_below="#+id/i2"
android:layout_marginLeft="2dp"
android:background="#drawable/bs"
/>
<Button
android:layout_width="220dp"
android:layout_height="50dp"
android:id="#+id/b3"
android:layout_below="#+id/t5"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
android:text="Add to Cart"
android:background="#color/colorOrange"
/>
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cheercreams.design">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".Main3Activity" />
<activity android:name=".Main4Activity" />
<activity android:name=".Main5Activity"></activity>
</application>
</manifest>
You defined Main2Activity in manifest file but you don't initiate Main2Activity class in package.
So, You able to go only from MainActivity to Main3Activity but not to Main2Activity.
Related
I am trying to implement a true caller like alert dialog in flutter. Since I am very new to flutter+dart, I was looking for any out of the box implementation. So far I have found system_alert_window but this creates the alert dialogue in JAVA and uses platform channels to communicate with it. My question is that if it's possible to do this is pure flutter+dart.
I do not know if this kind of dialogues has any special names, also in truecaller the dialogue is shown regardless the app is running or not. Can anyone provide some pointers?
Thanks
No you can't do with flutter
You can only go with Java.
Flollow the steps
your project > android > app > src > main > AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
add this permission on your Androidmanifest.xml file
after adding the permission add the below receiver class
<receiver android:name=".CallReceiver"
android:enabled="true"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
the receiver class
ServiceReceiver.Java
public class ServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
System.out.println("incomingNumber : "+incomingNumber);
WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
View view = LayoutInflater.from(context).inflate(R.layout.popup_window, null); // Code for inflating xml layout
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
windowManager.addView(view, params);
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
add the below xml file on the folder
your project > android > app > src > main > res > layout > popup_window.xml
popup_window.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:background="#000000"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/popup_color"
android:orientation="vertical"
tools:ignore="UselessParent">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/paymentlogo"
android:layout_width="65dp"
android:layout_height="65dp"
android:scaleType="fitCenter"
android:src="#drawable/rsoft_logo"
app:srcCompat="#drawable/rsoft_logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<TextView
android:id="#+id/lbl_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:layout_weight="0.3"
android:singleLine="true"
android:text="Anand"
android:textColor="#000000"
android:textSize="20sp" />
<ImageView
android:id="#+id/iv_close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.7"
android:src="#drawable/ic_close_black" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/txt_record_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone"
tools:ignore="HardcodedText" />
<View
android:id="#+id/btw_view"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_gravity="center"
android:background="#bab9b9" />
<LinearLayout
android:id="#+id/details_adp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/tv_icon"
android:layout_width="17dp"
android:layout_marginTop="4dp"
android:layout_height="17dp"
android:layout_weight="1"
android:textColor="#000000"
android:src="#android:drawable/sym_call_incoming"
android:textSize="10sp" />
<TextView
android:id="#+id/tv_mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:text="7667673691"
android:padding="0dp"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="#+id/tv_mobile_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:text="Incomingg"
android:textColor="#000000"
android:textSize="12sp" />
<!-- <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="4dp"
android:padding="3dp">
<ImageView
android:id="#+id/iv_im"
android:layout_width="14dp"
android:layout_height="14dp"
android:src="#drawable/sim" />
<TextView
android:id="#+id/tv_sim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="Incomingg"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>-->
</LinearLayout>
<TextView
android:id="#+id/tv_lead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="Save As Lead"
android:background="#drawable/rounded_btn"
android:padding="8dp"
android:layout_marginTop="6dp"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
You can use flutter_overlay_window. Its JAVA only but works fine. The example app has a truecaller like overlay demo. The other answer is correct, no it can't be done in pure flutter.
This question already has answers here:
findViewById returns null
(4 answers)
Can not find a View with findViewById()
(4 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
My apps unfortunately stopped after right I put the OnClickListener
My problem shows :
at com.example.whatisthat.Dashboard.onCreate(Dashboard.java:23)
Error :
LoginBtn.setOnClickListener(this);
Dashboard.java
package com.example.whatisthat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Dashboard extends AppCompatActivity implements View.OnClickListener
{
private CardView LoginBtn, CaptureBtn, AboutBtn, FeedbackBtn, IgBtn, HelpBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//DEFINE CARDS
//ADD CLICK LISTENER TO THE CARDS
LoginBtn = findViewById(R.id.Login);
LoginBtn.setOnClickListener(this);
CaptureBtn = findViewById(R.id.Capture);
CaptureBtn.setOnClickListener(this);
AboutBtn = findViewById(R.id.About);
AboutBtn.setOnClickListener(this);
FeedbackBtn = findViewById(R.id.Feedback);
FeedbackBtn.setOnClickListener(this);
IgBtn = findViewById(R.id.Ig);
IgBtn.setOnClickListener(this);
HelpBtn = findViewById(R.id.Help);
HelpBtn.setOnClickListener(this);
}
#Override
public void onClick(View view)
{
Intent i;
switch (view.getId())
{
case R.id.Login : i = new Intent(this,Login1.class);
startActivity(i);
break;
case R.id.Capture : i = new Intent(this, Capture.class);
startActivity(i);
break;
case R.id.About : i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.Feedback : i = new Intent(this, Feedback.class);
startActivity(i);
break;
case R.id.Ig : i = new Intent(this, Instagram.class);
startActivity(i);
break;
case R.id.Help : i = new Intent(this, Help.class);
startActivity(i);
break;
default:break;
}
}
}
activity_dashboard.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=".Dashboard">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:orientation="vertical"
android:gravity="center"
android:background="#6A287E">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/iconfyp"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DASHBOARD"
android:textColor="#color/colorAccent"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="215dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:id="#+id/Login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView1"
android:layout_height="50dp"
android:src="#drawable/people"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN"
android:textSize="16sp"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonLogin"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/Capture"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView2"
android:layout_height="50dp"
android:src="#drawable/cameraa"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAPTURE"
android:textSize="16sp"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonCapture"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:id="#+id/About"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView3"
android:layout_height="50dp"
android:src="#drawable/aboutt"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABOUT"
android:textSize="16sp"
android:layout_below="#+id/imageView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonAbout"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/Feedback"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView4"
android:layout_height="50dp"
android:src="#drawable/feedback"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FEEDBACK "
android:textSize="16sp"
android:layout_below="#+id/imageView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonFeedback"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:id="#+id/Ig"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView5"
android:layout_height="50dp"
android:src="#drawable/instagram"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INSTAGRAM"
android:textSize="16sp"
android:layout_below="#+id/imageView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonInstagram"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/Help"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView6"
android:layout_height="50dp"
android:src="#drawable/help"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELP"
android:textSize="16sp"
android:layout_below="#+id/imageView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonHelp"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Help"
android:label="#string/Help"/>
<activity android:name=".Instagram"
android:label="#string/Instagram"/>
<activity android:name=".Feedback"
android:label="#string/Feedback"/>
<activity android:name=".About"
android:label="#string/About"/>
<activity android:name=".Capture"
android:label="#string/Capture"/>
<activity android:name=".Login1"
android:label="#string/Login1"/>
<activity android:name=".Dashboard"
android:label="#string/Dashboard"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
In your onCreate() method there isn't
setContentView(R.layout.activity_dashboard);
before the methods findViewById() which return null.
Use:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard); //<-- add the method
LoginBtn = findViewById(R.id.Login);
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Null pointer Exception - findViewById()
(12 answers)
Closed 4 years ago.
I'm sorry if i'm not so accurate but this is my first app.
The problem is: when i press a button to change activity the app crash.
Can someone help me? Thanks for all!!
If you wanna have more information tell me and I try to explain better.
CODE:
MAIN ACTIVITY
package com.example.principale.designazioni;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button invia = (Button) findViewById(R.id.buttInvia);
invia.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivityPage1();
}
});
}
public void openActivityPage1() {
Intent intent = new Intent (getApplicationContext(), Page1.class);
startActivity(intent);
}
}
XML MAIN ACTIVITY
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="73dp">
<ImageView
android:id="#+id/imageView4"
android:layout_width="231dp"
android:layout_height="229dp"
android:layout_marginEnd="8dp"
android:adjustViewBounds="false"
android:contentDescription='#string tools:ignore="ContentDescription"'
android:cropToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.146"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04"
app:srcCompat="#mipmap/logo_grigio" />
<TextView
android:id="#+id/textView2"
android:layout_width="272dp"
android:layout_height="51dp"
android:fontFamily="monospace"
android:text="Disponibilità"
android:textAlignment="center"
android:textColor="#android:color/holo_orange_dark"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.818"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4"
app:layout_constraintVertical_bias="0.093" />
<Button
android:id="#+id/buttInvia"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="Invia"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#android:color/holo_orange_dark"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintVertical_bias="0.482" />
</android.support.constraint.ConstraintLayout>
ACTIVITY PAGE1
package com.example.principale.designazioni;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Page1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
EditText mail = (EditText) findViewById(R.id.editTextMail);
EditText nome = (EditText) findViewById(R.id.editTextNome);
EditText cognome = (EditText) findViewById(R.id.editTextCognome);
EditText tessera = (EditText) findViewById(R.id.editTextTessera);
Button avanti = (Button) findViewById(R.id.buttInvia);
Bundle bundle = new Bundle();
bundle.putString("mail", mail.getText().toString());
bundle.putString("nome", nome.getText().toString());
bundle.putString("cognome", cognome.getText().toString());
bundle.putString("tessera", tessera.getText().toString());
avanti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivityPage2();
}
});
}
public void openActivityPage2() {
Intent intent = new Intent (getApplicationContext(), Page2.class);
startActivity(intent);
}
}
XML ACTIVITY PAGE1
<?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=".Page1">
<TextView
android:id="#+id/textView3"
android:layout_width="71dp"
android:layout_height="27dp"
android:fontFamily="monospace"
android:text="E-Mail"
android:textAlignment="textStart"
android:textColor="#android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06" />
<TextView
android:id="#+id/textView4"
android:layout_width="96dp"
android:layout_height="27dp"
android:fontFamily="monospace"
android:text="Cognome"
android:textAlignment="textStart"
android:textColor="#android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.102"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.44" />
<TextView
android:id="#+id/textView6"
android:layout_width="71dp"
android:layout_height="27dp"
android:fontFamily="monospace"
android:text="Numero di Tessera"
android:textAlignment="textStart"
android:textColor="#android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.629" />
<TextView
android:id="#+id/textView5"
android:layout_width="71dp"
android:layout_height="27dp"
android:fontFamily="monospace"
android:text="Nome"
android:textAlignment="textStart"
android:textColor="#android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.251" />
<EditText
android:id="#+id/editTextMail"
android:layout_width="306dp"
android:layout_height="50dp"
android:ems="10"
android:fontFamily="monospace"
android:inputType="textEmailAddress"
android:textColor="#android:color/background_light"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editTextCognome"
android:layout_width="306dp"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextNome"
app:layout_constraintVertical_bias="0.17" />
<EditText
android:id="#+id/editTextNome"
android:layout_width="306dp"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editTextTessera"
android:layout_width="306dp"
android:layout_height="50dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="Avanti"
android:textAlignment="center"
android:textColor="#android:color/holo_orange_dark"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextTessera"
app:layout_constraintVertical_bias="0.487" />
</android.support.constraint.ConstraintLayout>
ACTIVITY PAGE2
package com.example.principale.designazioni;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Page2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
final TextView nome = (TextView) findViewById(R.id.textViewPage2);
Bundle bundle = this.getIntent().getExtras();
nome.setText(bundle.getString("nome"));
}
}
XML ACTIVITY PAGE2
<?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=".Page2">
<TextView
android:id="#+id/textViewPage2"
android:layout_width="180dp"
android:layout_height="47dp"
android:fontFamily="monospace"
android:text="TextView"
android:textAlignment="center"
android:textColor="#android:color/holo_orange_dark"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
ANDROID MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.principale.designazioni">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Page1" />
<activity android:name=".Page2"></activity>
</application>
</manifest>
STACK TRACE
08-24 10:21:07.370 6819-6819/com.example.principale.designazioni E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.principale.designazioni, PID: 6819
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.principale.designazioni/com.example.principale.designazioni.Page1}: 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:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
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.example.principale.designazioni.Page1.onCreate(Page1.java:30)
at android.app.Activity.performCreate(Activity.java:7372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Change this line in Page1.java
from
Button avanti = (Button) findViewById(R.id.buttInvia);
to
Button avanti = (Button) findViewById(R.id.button);
Happy day to all of you.
I have been attempting to make a stopwatch application as my first one, while using the Chronometer function. The problem is, when I click on the Start button (while the app is running on tablet), it crashes. The situation is similar to the others.
I am not sure how I should fix this, and I greatly appreciate your help.
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">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<ImageView
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#color/colorPrimaryDark" />
<LinearLayout
android:id="#+id/navcon"
android:layout_width="match_parent"
android:layout_height="92dp"
android:orientation="vertical">
<ImageView
android:id="#+id/navbar"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/colorPrimary" />
</LinearLayout>
<LinearLayout
android:id="#+id/navtextcon"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="vertical">
<TextView
android:id="#+id/navtext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="rocket.studios"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:id="#+id/chronocon"
android:layout_width="match_parent"
android:layout_height="95dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="78dp"
android:orientation="vertical">
<Chronometer
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="60sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/startbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="155dp"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="#+id/startb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#color/colorPrimary"
android:text="#string/start"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/stopbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="#+id/stopb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#color/colorPrimary"
android:text="#string/stop"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/resetbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="158dp"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="#+id/resetb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#color/colorPrimary"
android:text="#string/reset"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
MainActivity.java :
package testing.ezekieralt.com.stopwatcher;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.os.SystemClock;
//chronometer is...guess it
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Chronometer chronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.startb).setOnClickListener(this);
findViewById(R.id.resetb).setOnClickListener(this);
findViewById(R.id.stopb).setOnClickListener(this);
chronometer = findViewById(R.id.chronometer);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.stopb:
chronometer.stop();
break;
case R.id.startb:
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
break;
case R.id.resetb:
chronometer.setBase(SystemClock.elapsedRealtime());
break;
}
}
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="testing.ezekieralt.com.stopwatcher">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You didn't added id to your Chronometer. Add id to your Chronometer
<Chronometer
android:id="#+id/chronometer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="60sp"
android:textStyle="bold" />
My project on android studio is running fine on few phones and emulators but crashes on many phones. I have a Main_Activity and clicking on login takes to a second activity. I am sharing the java files and xml files of both activities along with android manifest.
Main Activity Java file
package club.pithynews.iitbhilai_pithy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText password;
private Button login_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoginButton();
}
public void LoginButton() {
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
login_button = (Button) findViewById(R.id.button);
login_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((username.getText().toString().equals("user"))&&(password.getText().toString().equals("pass"))) {
Intent intent = new Intent("club.pithynews.iitbhilai_pithy.User");
startActivity(intent);
}
}
}
);
}
}
Main Activity XML file
<?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"
android:background="#drawable/back_login"
tools:context="club.pithynews.iitbhilai_pithy.MainActivity">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#11000000"
android:ems="10"
android:hint="#string/user_name"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.78"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.53" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#11000000"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.78"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.64" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.53"
app:srcCompat="#drawable/user1"
android:contentDescription="#string/todo" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.06"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.64"
app:srcCompat="#drawable/lock1"
android:contentDescription="#string/todo" />
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/back_login"
android:text="#string/login"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.79" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="233dp"
android:layout_height="227dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="1dp"
android:layout_marginStart="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.43"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057"
app:srcCompat="#drawable/logoiit"
android:contentDescription="#string/todo" />
</android.support.constraint.ConstraintLayout >
Second Activity Java file
package club.pithynews.iitbhilai_pithy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class User extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
}
}
Second Activity XML file
<?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"
android:background="#drawable/white"
tools:context="club.pithynews.iitbhilai_pithy.User">
<ImageButton
android:id="#+id/imageButton_emergency3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="80dp"
android:maxHeight="100dp"
android:scaleType="fitCenter"
android:layout_marginLeft="270dp"
android:layout_marginTop="350dp"
android:src="#drawable/emergency3"
android:background="#null"
android:layout_marginStart="270dp"
android:contentDescription="#string/todo13" />
<ImageButton
android:id="#+id/imageButton_feedback1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="125dp"
android:maxHeight="100dp"
android:scaleType="fitCenter"
android:layout_marginLeft="265dp"
android:layout_marginTop="445dp"
android:src="#drawable/feedback1"
android:background="#null"
android:layout_marginStart="265dp"
android:contentDescription="#string/todo12" />
<ImageButton
android:id="#+id/imageButton_aboutus1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="125dp"
android:maxHeight="500dp"
android:scaleType="fitCenter"
android:layout_marginLeft="125dp"
android:layout_marginTop="455dp"
android:src="#drawable/aboutus1"
android:background="#null"
android:layout_marginStart="125dp"
android:contentDescription="#string/todo11" />
<ImageButton
android:id="#+id/imageButton_campusmap1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="125dp"
android:maxHeight="80dp"
android:scaleType="fitCenter"
android:layout_marginLeft="20dp"
android:layout_marginTop="455dp"
android:src="#drawable/campusmap1"
android:background="#null"
android:layout_marginStart="20dp"
android:contentDescription="#string/todo10" />
<ImageButton
android:id="#+id/imageButton_adminstrators"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="100dp"
android:maxHeight="500dp"
android:scaleType="fitCenter"
android:layout_marginLeft="140dp"
android:layout_marginTop="350dp"
android:src="#drawable/adminstrators"
android:background="#null"
android:layout_marginStart="140dp"
android:contentDescription="#string/todo9" />
<ImageButton
android:id="#+id/imageButton_fnr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="350dp"
android:adjustViewBounds="true"
android:background="#null"
android:contentDescription="#string/fee_and_registration"
android:maxHeight="200dp"
android:maxWidth="75dp"
android:scaleType="fitCenter"
android:src="#drawable/fnr"
android:layout_marginStart="25dp" />
<ImageButton
android:id="#+id/imageButton_clubs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="265dp"
android:layout_marginTop="220dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/clubs"
android:layout_marginStart="265dp"
android:contentDescription="#string/todo8" />
<ImageButton
android:id="#+id/imageButton_studdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="135dp"
android:layout_marginTop="220dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/studdata"
android:layout_marginStart="135dp"
android:contentDescription="#string/todo7" />
<ImageButton
android:id="#+id/imageButton_bus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="220dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/bus"
android:layout_marginStart="15dp"
android:contentDescription="#string/todo6" />
<ImageButton
android:id="#+id/imageButton_mess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="265dp"
android:layout_marginTop="100dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/mess"
android:layout_marginStart="265dp"
android:contentDescription="#string/todo5" />
<ImageButton
android:id="#+id/imageButton_faculty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="135dp"
android:layout_marginTop="100dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/faculty"
android:layout_marginStart="135dp"
android:contentDescription="#string/todo4" />
<ImageButton
android:id="#+id/imageButton_academics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="100dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/academics"
android:layout_marginStart="15dp"
android:contentDescription="#string/todo3" />
<ImageButton
android:id="#+id/imageButton_newsletter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="265dp"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/news"
android:layout_marginStart="265dp"
android:contentDescription="#string/todo" />
<ImageButton
android:id="#+id/imageButton_events"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="135dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/events"
android:layout_marginStart="135dp"
android:contentDescription="#string/todo1" />
<ImageButton
android:id="#+id/imageButton_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:background="#null"
android:maxHeight="500dp"
android:maxWidth="100dp"
android:scaleType="fitCenter"
android:src="#drawable/today"
android:layout_marginStart="15dp"
android:contentDescription="#string/todo2" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignEnd="#+id/imageButton_emergency3"
android:layout_alignRight="#+id/imageButton_emergency3"
android:text="#string/textview5"
android:textColor="#color/colorPrimaryDark"
android:visibility="visible"
tools:text=" Emergency " />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignEnd="#+id/imageButton_adminstrators"
android:layout_alignRight="#+id/imageButton_adminstrators"
android:text="#string/administrators"
android:textColor="#color/colorPrimaryDark" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton_bus"
android:layout_alignStart="#+id/imageButton_bus"
android:layout_below="#+id/imageButton_fnr"
android:text="#string/textview4"
android:textColor="#color/colorPrimaryDark"
tools:text=" Fees and\n Registration" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignStart="#+id/textView3"
android:layout_below="#+id/imageButton_campusmap1"
android:text="#string/textview3"
tools:text=" Campus Map"
android:textColor="#color/colorPrimaryDark"/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton_adminstrators"
android:layout_alignStart="#+id/imageButton_adminstrators"
android:layout_below="#+id/imageButton_campusmap1"
android:text="#string/textview2"
tools:text=" About Us"
android:textColor="#color/colorPrimaryDark"/>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton_feedback1"
android:layout_alignStart="#+id/imageButton_feedback1"
android:layout_alignTop="#+id/textView5"
android:text="#string/textview1"
tools:text=" Feedback"
android:textColor="#color/colorPrimaryDark"/>
</RelativeLayout>
Android Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="club.pithynews.iitbhilai_pithy">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".User">
<intent-filter>
<action android:name="club.pithynews.iitbhilai_pithy.User" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
</resources>
Debug Report when app crashes
W/System: ClassLoader referenced unknown path: /data/app/club.pithynews.iitbhilai_pithy-1/lib/arm
I/InstantRun: starting instant run server: is main process
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
W/ResourceType: Failure getting entry for 0x7f06005a (t=5 e=90) (error -75)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: club.pithynews.iitbhilai_pithy, PID: 16468
java.lang.RuntimeException: Unable to start activity ComponentInfo{club.pithynews.iitbhilai_pithy/club.pithynews.iitbhilai_pithy.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.constraint.ConstraintLayout
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.constraint.ConstraintLayout
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.constraint.ConstraintLayout
at android.app.Activity.performCreate(Activity.java:6904)
at a
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.content.res.Resources$NotFoundException: Resource "club.pithynews.iitbhilai_pithy:drawable/back_login" (7f06005a) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f06005a a=-1 r=0x7f06005a}
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Error is self explanatory. You're trying to load an image that is too big.
Canvas: trying to draw too large(111300000bytes) bitmap.
One of your drawable source that you set on your ImageButton or ImageView is too big. You can:
Scale image manually
Perform scaling in code and set the image afterwards
java.lang.RuntimeException: Canvas: trying to draw too large(111300000bytes) bitmap.
As you can see, you are trying to display an image that is too large. Check the size of the included bitmap resources.
you are displaying an image of size 111.3 mb which is too large to handle you need to compress and scale to some smaller version before displaying.
here you can scale your image.
Bitmap scaled = Bitmap.createScaledBitmap(originalBitmap, 120, 120, false);
more here
and compress using
ByteArrayOutputStream out = new ByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);
see more here