I am new to android. Here am trying to pass the radiobutton and Checkbox values from one activity to another activity. Its not showing any error but when I run it simply gets closed. Any help would be appreciated.Here I have posted the complete code..
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
public class MainActivity extends Activity {
Intent obj = new Intent(this, SecondActivity.class);
Bundle extras = new Bundle();
RadioButton male, female, single, married;
CheckBox twelveth, bE, mE;
String s1="+2";
String s2="B.E.";
String s3="M.E.";
StringBuilder s;
String str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
single = (RadioButton) findViewById(R.id.single);
married = (RadioButton) findViewById(R.id.married);
twelveth = (CheckBox) findViewById(R.id.twelveth);
bE = (CheckBox) findViewById(R.id.BE);
mE = (CheckBox) findViewById(R.id.ME);
str=s.toString();
extras.putString("Education",str);
}
public void onGenderClicked(View v) {
boolean checked = ((RadioButton) v).isChecked();
switch (v.getId()) {
case R.id.male:
if (checked)
extras.putString("Gender", "Male");
break;
case R.id.female:
if (checked)
extras.putString("Gender", "Female");
break;
}
}
public void onStatusClicked(View v) {
boolean checked = ((RadioButton) v).isChecked();
switch (v.getId()) {
case R.id.single:
if (checked)
extras.putString("Status", "Single");
break;
case R.id.married:
if (checked)
extras.putString("Status", "Married");
break;
}
}
public void ButtonClick(View v) {
obj.putExtras(extras);
startActivity(obj);
}
public void onCheckBoxClicked(View v)
{
boolean checked=((CheckBox)v).isChecked();
switch(v.getId())
{
case R.id.twelveth:
if(checked)
{
s.append("\n"+s1);
}
break;
case R.id.BE:
if(checked)
{
s.append("\n"+s2);
}
break;
case R.id.ME:
if(checked)
{
s.append("\n"+s3);
}
break;
}
str=s.toString();
extras.putString("Education",str);
}
}
SecondActivity
package com.example.aravindpraveen.buttonsandboxes;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
TextView tvGender,tvStatus,tvEducation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tvGender=(TextView)findViewById(R.id.tvGender);
tvStatus=(TextView)findViewById(R.id.tvStatus);
tvEducation=(TextView)findViewById(R.id.tvEducation);
Intent obj1=getIntent();
Bundle extras=obj1.getExtras();
String gender=extras.getString("Gender");
String status=extras.getString("Starus");
String education=extras.getString("Education");
tvGender.setText(gender);
tvStatus.setText(status);
tvEducation.setText(education);
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aravindpraveen.buttonsandboxes" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#6457d962"
android:orientation="vertical">
<TextView android:text="#string/b_b"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="40sp"
android:background="#f62dd93c"
android:textColor="#c34c27e6"
android:gravity="center"/>
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/gender"
android:textSize="40sp"
android:gravity="center"
android:textColor="#000000"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rg_gender">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/male"
android:text="#string/male"
android:textSize="30sp"
android:checked="true"
android:onClick="onGenderClicked"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/female"
android:text="#string/female"
android:textSize="30sp"
android:onClick="onGenderClicked"/>
</RadioGroup>
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="#string/M_status"
android:textColor="#000000"
android:gravity="center"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rg_status">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/single"
android:text="#string/single"
android:textSize="30sp"
android:checked="true"
android:onClick="onStatusClicked"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/married"
android:text="#string/married"
android:textSize="30sp"
android:onClick="onStatusClicked"/>
</RadioGroup>
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="#string/education"
android:textColor="#000000"
android:gravity="center"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/twelveth"
android:textSize="30sp"
android:id="#+id/twelveth"
android:onClick="onCheckBoxClicked"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/bE"
android:textSize="30sp"
android:id="#+id/BE"
android:onClick="onCheckBoxClicked"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/mE"
android:textSize="30sp"
android:id="#+id/ME"
android:onClick="onCheckBoxClicked"/>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/submit"
android:text="#string/submit"
android:layout_gravity="center"
android:onClick="ButtonClick"/>
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4dbde"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:background="#fba1a1"
android:text="#string/heading"
android:gravity="center"
android:textColor="#ffffff"/>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/gender"
android:textSize="40sp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#f7beae"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingTop="20dp"
android:gravity="center"
android:id="#+id/tvGender"/>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/M_status"
android:textSize="40sp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#f7beae"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingTop="20dp"
android:gravity="center"
android:id="#+id/tvStatus"/>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/education"
android:textSize="40sp"
android:gravity="center"
android:textColor="#ffffff"
android:background="#f7beae"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingTop="20dp"
android:gravity="center"
android:id="#+id/tvEducation"/>
</LinearLayout>
LogCat
02-03 11:39:42.859 1135-1135/com.example.aravindpraveen.buttonsandboxes D/AndroidRuntime﹕ Shutting down VM
02-03 11:39:42.863 1135-1135/com.example.aravindpraveen.buttonsandboxes W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c2c648)
02-03 11:39:42.899 1135-1135/com.example.aravindpraveen.buttonsandboxes E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.aravindpraveen.buttonsandboxes/com.example.aravindpraveen.buttonsandboxes.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3662)
at com.example.aravindpraveen.buttonsandboxes.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
from the second activity declare in Manifest, remove tags
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
i think starting activity as MAIN while it's not MAIN may cause this problem
also in documentaion
Activity Action: Start as a main entry point, does not expect to receive data.
EDIT:
also move
str=s.toString();
extras.putString("Education",str);
inside onClick that starts the 2nd activity
Related
When I click the cardView with the id addNotice in the activity_main.xml the second Activity UploadNotice is supposed to show up, but my Android studio app closes and the second Activity that I've created never shows up. I'm new to Android Studio and Java so I would appreciate it a lot if anyone can help me!
Here is my MainActivity.java :
package com.example.adminuniversityapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
CardView uploadNptice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uploadNptice = findViewById(R.id.addNotice);
uploadNptice.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.addNotice:
Intent intent = new Intent( MainActivity.this,UploadNotice.class);
startActivity(intent);
break;
}
}
}
and Here is my activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="horizontal">
<com.google.android.material.card.MaterialCardView
android:id="#+id/addNotice"
android:layout_width="130dp"
android:layout_height="150dp"
android:layout_margin="10dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="135dp"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/circle_green"
android:padding="15dp"
android:src="#drawable/ic_notice" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/lightGray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Upload Notice"
android:textColor="#color/textColor"
android:textStyle="bold" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/addGalleryImage"
android:layout_width="130dp"
android:layout_height="150dp"
android:layout_margin="10dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/circle_purple"
android:padding="15dp"
android:src="#drawable/ic_gallery"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/lightGray"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Upload Image"
android:textColor="#color/textColor"
android:textStyle="bold"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="horizontal">
<com.google.android.material.card.MaterialCardView
android:id="#+id/addEbook"
android:layout_width="130dp"
android:layout_height="150dp"
android:layout_margin="10dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/circle_pink"
android:padding="15dp"
android:src="#drawable/ic_upload"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/lightGray"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Upload Ebook"
android:textColor="#color/textColor"
android:textStyle="bold"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/faculty"
android:layout_width="130dp"
android:layout_height="150dp"
android:layout_margin="10dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/circle_yellow"
android:padding="15dp"
android:src="#drawable/ic_group"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/lightGray"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Update Faculty"
android:textColor="#color/textColor"
android:textStyle="bold"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="horizontal">
<com.google.android.material.card.MaterialCardView
android:id="#+id/deleteNotice"
android:layout_width="130dp"
android:layout_height="150dp"
android:layout_margin="10dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/cicle_red"
android:padding="15dp"
android:src="#drawable/ic_delete"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/lightGray"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Delete Notice"
android:textColor="#color/textColor"
android:textStyle="bold"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</LinearLayout>
Also here is the AndroidManifest.xml just in case :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.adminuniversityapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<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/Theme.AdminUniversityApp">
<activity android:name=".UploadNotice" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the second Activity UploadNotice.java:
package com.example.adminuniversityapp;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;
public class UploadNotice extends AppCompatActivity {
private CardView addImage;
private ImageView noticeImageView;
private final int REQ = 1;
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_notice);
addImage = findViewById(R.id.addImage);
noticeImageView = findViewById(R.id.noticeImageView);
addImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() {
Intent pickImage = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickImage, REQ);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ && resultCode == RESULT_OK){
Uri uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
} catch (IOException e) {
e.printStackTrace();
}
noticeImageView.setImageBitmap(bitmap);
}
}
}
and Here is the second Activity Xml code :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".UploadNotice"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="#+id/addImage"
android:layout_width="130dp"
android:layout_height="150dp"
android:layout_margin="10dp"
app:cardElevation="5dp"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/circle_purple"
android:padding="15dp"
android:src="#drawable/ic_image"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/lightGray"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Select Image"
android:textColor="#color/textColor"
android:textStyle="bold"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.Material3.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/noticeTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Notice title" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Upload Notice"
android:layout_marginTop="16dp"
android:textAllCaps="false"
android:id="#+id/uploadNoticeBtn"/>
<com.google.android.material.card.MaterialCardView
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="400dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:id="#+id/noticeImageView"/>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</ScrollView>
And this is the Logcat when I click the Cardview button
2022-02-26 16:09:00.615 26302-26302/com.example.adminuniversityapp I/Timeline: Timeline: Activity_launch_request time:449776768
2022-02-26 16:09:00.633 26302-26357/com.example.adminuniversityapp V/FA: Recording user engagement, ms: 60057
2022-02-26 16:09:00.636 26302-26357/com.example.adminuniversityapp V/FA: Using local app measurement service
2022-02-26 16:09:00.641 26302-26357/com.example.adminuniversityapp V/FA: Activity paused, time: 598904714
2022-02-26 16:09:00.651 26302-26302/com.example.adminuniversityapp V/FA: onActivityCreated
2022-02-26 16:09:00.656 26302-26302/com.example.adminuniversityapp D/DecorView[]: getWindowModeFromSystem windowmode is 1
2022-02-26 16:09:00.667 26302-26357/com.example.adminuniversityapp V/FA: Connection attempt already in progress
2022-02-26 16:09:00.681 26302-26302/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-02-26 16:09:00.687 26302-26302/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-02-26 16:09:00.693 26302-26302/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-02-26 16:09:00.722 26302-26302/com.example.adminuniversityapp D/AndroidRuntime: Shutting down VM
2022-02-26 16:09:00.724 26302-26302/com.example.adminuniversityapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.adminuniversityapp, PID: 26302
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adminuniversityapp/com.example.adminuniversityapp.UploadNotice}: android.view.InflateException: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3580)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3752)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2191)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8049)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
Caused by: android.view.InflateException: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:856)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1012)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1142)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1103)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1145)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1103)
at android.view.LayoutInflater.inflate(LayoutInflater.java:684)
at android.view.LayoutInflater.inflate(LayoutInflater.java:536)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.adminuniversityapp.UploadNotice.onCreate(UploadNotice.java:32)
at android.app.Activity.performCreate(Activity.java:8142)
at android.app.Activity.performCreate(Activity.java:8114)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1308)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3553)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3752)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2191)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8049)
at java.lang.reflect.Method.invoke(Native Method)
2022-02-26 16:09:00.725 26302-26302/com.example.adminuniversityapp E/AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:185)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:116)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:474)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:433)
... 30 more
2022-02-26 16:09:00.749 26302-26302/com.example.adminuniversityapp I/Process: Sending signal. PID: 26302 SIG: 9
there are two themes.xml :
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AdminUniversityApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AdminUniversityApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_200</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_200</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
In the styles.xml file, change the theme to something like this:
<style name="Theme.AdminUniversityApp" parent="Theme.Material3.DayNight">
...
</style>
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 4 years ago.
I made a quiz game with codes I found on github because I'm a newbie and I want to learn programming.
So, when I press the start button game crashes. I tried it without the other classes but it still crashes. There is my MainActivity class, MainActivity xml and AndroidManifest.
MainActivity.java
package com.proje.bilgiyarismasi.bilgiyarismasi;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton baslat,ayarlar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baslat = findViewById(R.id.baslatButonu);
ayarlar = findViewById(R.id.ayarlarButonu);
baslat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SoruAlani.class);
startActivity(intent);
finish();
}
});
ayarlar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,Ayarlar.class);
startActivity(intent);
finish();
}
});
}
}
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"
android:background="#color/deepPurple"
tools:context="com.proje.bilgiyarismasi.bilgiyarismasi.MainActivity"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="#+id/logoIsim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="#string/logoIsim"
android:textColor="#color/orenji"
android:textSize="50sp"
tools:layout_editor_absoluteX="150dp"
tools:layout_editor_absoluteY="28dp" />
<ImageButton
android:id="#+id/baslatButonu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logoIsim"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
app:srcCompat="#drawable/menu_1"
android:adjustViewBounds="true"
/>
<ImageView
android:id="#+id/playButton"
android:layout_width="72dp"
android:layout_height="72dp"
app:srcCompat="#drawable/play_button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/baslatButonu"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="2dp"
android:gravity="center"/>
<TextView
android:id="#+id/basla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/baslatButonu"
android:layout_alignEnd="#id/baslatButonu"
android:layout_alignLeft="#id/baslatButonu"
android:layout_alignRight="#id/baslatButonu"
android:layout_alignStart="#id/baslatButonu"
android:layout_alignTop="#id/baslatButonu"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/baslat"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold"
/>
<ImageButton
android:id="#+id/ayarlarButonu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/baslatButonu"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
app:srcCompat="#drawable/menu_1"
android:adjustViewBounds="true"
/>
<TextView
android:id="#+id/ayarlar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/ayarlarButonu"
android:layout_alignEnd="#id/ayarlarButonu"
android:layout_alignLeft="#id/ayarlarButonu"
android:layout_alignRight="#id/ayarlarButonu"
android:layout_alignStart="#id/ayarlarButonu"
android:layout_alignTop="#id/ayarlarButonu"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/ayarlar"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/ayarla"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignBottom="#id/ayarlarButonu"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="2dp"
app:srcCompat="#drawable/settings_button"
android:adjustViewBounds="true"
/>
<TextView
android:id="#+id/sosyalMedya"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ayarlarButonu"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="#string/sosyalMedya"
android:textAlignment="center"
android:textStyle="bold"
android:gravity="center"/>
<ImageButton
android:id="#+id/facebookLogo"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignTop="#id/sosyalMedya"
app:srcCompat="#drawable/facebook_logo"
android:adjustViewBounds="true"
android:background="#null"
android:scaleType="fitCenter"
android:layout_toLeftOf="#id/sosyalMedya"
android:layout_toStartOf="#id/sosyalMedya"
android:layout_marginTop="30dp"
/>
<ImageButton
android:id="#+id/twitterLogo"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignTop="#+id/sosyalMedya"
app:srcCompat="#drawable/twitter_logo"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#null"
android:layout_centerInParent="true"
android:layout_marginTop="30dp"
/>
<ImageButton
android:id="#+id/slackLogo"
android:layout_width="72dp"
android:layout_height="72dp"
app:srcCompat="#drawable/slack_logo"
android:layout_alignTop="#+id/sosyalMedya"
android:adjustViewBounds="true"
android:background="#null"
android:scaleType="fitCenter"
android:layout_toRightOf="#id/sosyalMedya"
android:layout_toEndOf="#id/sosyalMedya"
android:layout_marginTop="30dp"
/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.proje.bilgiyarismasi.bilgiyarismasi">
<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=".TekrarOyna" />
<activity android:name=".OyunKazanma" />
<activity android:name=".SoruAlani" />
<activity android:name=".SureBitti">
</activity>
</application>
</manifest>
logcat
05-25 12:28:18.414 6393-6393/com.proje.bilgiyarismasi.bilgiyarismasi
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.proje.bilgiyarismasi.bilgiyarismasi, PID: 6393
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.proje.bilgiyarismasi.bilgiyarismasi/com.proje.bilgiyarismasi.bilgiyarismasi.SoruAlani}:
java.lang.RuntimeException: Font asset not found fonts/Bariol_Regular.otf
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.RuntimeException: Font asset not found
fonts/Bariol_Regular.otf
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.proje.bilgiyarismasi.bilgiyarismasi.SoruAlani.onCreate(SoruAlani.java:51)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-25 12:28:20.016 4207-6527/com.android.providers.calendar E/SQLiteLog:
(284) automatic index on view_events(_id)
05-25 12:28:26.129 1535-1602/system_process E/InputDispatcher: channel
'2cf4e81e
com.proje.bilgiyarismasi.bilgiyarismasi/com.proje.bilgiyarismasi.bilgiyarismasi.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
Thanks for your help.
You have not casted the image buttons correctly.
Use this in onCreate()
baslat = (ImageButton)findViewById(R.id.baslatButonu);
ayarlar =(ImageButton) findViewById(R.id.ayarlarButonu);
And Declare this activity in manifest
<activity android:name=".Ayarlar">
From the logcat
**java.lang.RuntimeException: Font asset not found fonts/Bariol_Regular.otf**
Add this font file also
Bariol_Regular.otf
Check this link please
How to use custom font in Android Studio
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" />
In my app, i have a SplashScreen activity. I have setStateLocked error when i clicked on the app. I'm using Fortune Ship MTN-S730 running android 5.1.
Here is the onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen); /* logcat says error on this line */
}
The says the says that error is on the line with
setContentView(R.layout.activity_splash_screen);
here is the output from the logcat
setStateLocked: wasEnabled = false, mIsEnabled = false, wasTouchExplorationEnabled = false,
mIsTouchExplorationEnabled = false, wasHighTextContrastEnabled = false, mIsHighTextContrastEnabled = false
java.lang.Throwable: setStateLocked
at android.view.accessibility.AccessibilityManager.setStateLocked(AccessibilityManager.java:553)
at android.view.accessibility.AccessibilityManager.tryConnectToServiceLocked(AccessibilityManager.java:636)
at android.view.accessibility.AccessibilityManager.<init>(AccessibilityManager.java:226)
at android.view.accessibility.AccessibilityManager.getInstance(AccessibilityManager.java:206)
at android.view.View.setFlags(View.java:9843)
at android.view.ViewGroup.initViewGroup(ViewGroup.java:536)
at android.view.ViewGroup.<init>(ViewGroup.java:525)
at android.view.ViewGroup.<init>(ViewGroup.java:520)
at android.view.ViewGroup.<init>(ViewGroup.java:516)
at android.view.ViewGroup.<init>(ViewGroup.java:512)
at android.widget.FrameLayout.<init>(FrameLayout.java:119)
at com.android.internal.policy.impl.PhoneWindow$DecorView.<init>(PhoneWindow.java:2326)
at com.android.internal.policy.impl.PhoneWindow.generateDecor(PhoneWindow.java:3460)
at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3846)
at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:2042)
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:371)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:320)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:281)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.cm_floraison.brandbook.gesu.SplashScreenActivity.onCreate(SplashScreenActivity.java:101)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Here is the manifest declaration of the activity
<activity
android:name=".gesu.SplashScreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Also the content of activity_splash_screen.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context="com.cm_toure.myapp.gesu.SplashScreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/fullscreen_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="#string/app_logo"
android:foregroundGravity="center"
android:keepScreenOn="true"
android:src="#drawable/ic_logo_brandbook_paysage_sur_fond_blanc"
/>
</LinearLayout>
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:orientation="vertical"
tools:ignore="UselessParent">
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="10dp"
android:indeterminate="true"
android:indeterminateDrawable="#drawable/splash_spinner_style"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:text="#string/powered_by"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
</FrameLayout>
This is my fourthpage class in my application. I am getting the error
unfortunately application has stopped working
I have no idea why am I getting this.
package student.a.a;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class fourthpage extends Activity {
EditText txtb1, txtb2, txtb3, txtb4, txtb5;
Button btnper;
TextView lblgrade, lblper;
DecimalFormat df = new DecimalFormat("##.##");
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourthxml);
// text boxes
txtb1 = (EditText) findViewById(R.id.txtb1);
txtb2 = (EditText) findViewById(R.id.txtb2);
txtb3 = (EditText) findViewById(R.id.txtb3);
txtb4 = (EditText) findViewById(R.id.txtb4);
txtb5 = (EditText) findViewById(R.id.txtb5);
// buttons
btnper = (Button) findViewById(R.id.btnper);
// labels/text view
lblgrade = (TextView) findViewById(R.id.lblgrade);
lblper = (TextView) findViewById(R.id.lblgrade);
}
String tb1 = txtb1.getText().toString();
String tb2 = txtb2.getText().toString();
String tb3 = txtb3.getText().toString();
String tb4 = txtb4.getText().toString();
String tb5 = txtb5.getText().toString();
double txb1 = Double.parseDouble(tb1);
double txb2 = Double.parseDouble(tb2);
double txb3 = Double.parseDouble(tb3);
double txb4 = Double.parseDouble(tb4);
double txb5 = Double.parseDouble(tb5);
double sum = 0;
double total = 0;
public void onClickper(View v) {
sum = txb1 + txb2 + txb3 + txb4 + txb5;
total = (sum / 500) * 100;
String tot = df.format(total);
lblper.setText(tot);
if (total <= 100) {
lblgrade.setText("Congratulations !! , Grade 'A+' ");
} else if (total <= 80) {
lblgrade.setText("Congratulations !! , Grade 'A'");
} else if (total <= 70) {
lblgrade.setText("Grade 'B'");
} else if (total <= 60) {
lblgrade.setText("Grade 'C'");
} else
lblgrade.setText("Failed");
}
}
This is the xml code of fourthpage. It contains 5 text boxes, a button and two textviews:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/gridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_gravity="left"
android:text="Calculate your marks"
android:textAppearance="?android:attr/textAppearanceMedium" />
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/gridLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Space android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/gridLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_gravity="left"
android:text="Subject 1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtb1"
android:layout_width="162dp"
android:layout_gravity="fill_horizontal" >
<requestFocus />
</EditText>
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/GridLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView01"
android:layout_gravity="left"
android:text="Subject 2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtb2"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/GridLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView02"
android:layout_gravity="left"
android:text="Subject 3"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtb3"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/GridLayout03"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView03"
android:layout_gravity="left"
android:text="Subject 4"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtb4"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/GridLayout04"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView04"
android:layout_gravity="left"
android:text="Subject 5"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtb5"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/gridLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnper"
android:layout_gravity="left"
android:text="Button" android:onClick="onClickper"/>
<Button
android:id="#+id/btngrade"
android:layout_gravity="left"
android:text="Button" />
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow9"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/gridLayout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_gravity="left"
android:text="percentage is "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/lblper"
android:layout_gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="#+id/gridLayout6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView5"
android:layout_gravity="left"
android:text=" Grade is "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/lblgrade"
android:layout_gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
</TableRow>
</TableLayout>
</LinearLayout>
Here is androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="student.a.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".StudentsinfoActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".secondpage" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".thirdpage" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".fourthpage" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my logcat:
08-09 16:32:00.970: W/dalvikvm(892): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-09 16:32:00.980: E/AndroidRuntime(892): FATAL EXCEPTION: main
08-09 16:32:00.980: E/AndroidRuntime(892): java.lang.RuntimeException: Unable to start activity ComponentInfo{student.a.a/student.a.a.fourthpage}: java.lang.NumberFormatException: Invalid double: ""
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.os.Looper.loop(Looper.java:137)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.reflect.Method.invoke(Method.java:511)
08-09 16:32:00.980: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-09 16:32:00.980: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-09 16:32:00.980: E/AndroidRuntime(892): at dalvik.system.NativeStart.main(Native Method)
08-09 16:32:00.980: E/AndroidRuntime(892): Caused by: java.lang.NumberFormatException: Invalid double: ""
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.Double.parseDouble(Double.java:295)
08-09 16:32:00.980: E/AndroidRuntime(892): at student.a.a.fourthpage.onCreate(fourthpage.java:61)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.Activity.performCreate(Activity.java:4465)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-09 16:32:00.980: E/AndroidRuntime(892): ... 11 more
Just because you have put your variable declarations after onCreate that does not mean this code will execute after onCreate. All your String members will be initialized before onCreate is called and before EditText members are initialized. Same goes for your double members.
String tb1 = txtb1.getText().toString();
String tb2 = txtb2.getText().toString();
String tb3 = txtb3.getText().toString();
String tb4 = txtb4.getText().toString();
String tb5 = txtb5.getText().toString();
double txb1= Double.parseDouble(tb1);
double txb2= Double.parseDouble(tb2);
double txb3= Double.parseDouble(tb3);
double txb4= Double.parseDouble(tb4);
double txb5= Double.parseDouble(tb5);
You have to move above initialization into onCreate and declare String and double members separately.
...
String tb1, tb2, tb3, tb4, tb5;
double txb1, txb2, txb, txb4, txb5;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourthxml);
//text boxes
txtb1=(EditText)findViewById(R.id.txtb1);
txtb2=(EditText)findViewById(R.id.txtb2);
txtb3=(EditText)findViewById(R.id.txtb3);
txtb4=(EditText)findViewById(R.id.txtb4);
txtb5=(EditText)findViewById(R.id.txtb5);
//buttons
btnper=(Button)findViewById(R.id.btnper);
//labels/text view
lblgrade=(TextView)findViewById(R.id.lblgrade);
lblper=(TextView)findViewById(R.id.lblgrade);
tb1 = txtb1.getText().toString();
tb2 = txtb2.getText().toString();
tb3 = txtb3.getText().toString();
tb4 = txtb4.getText().toString();
tb5 = txtb5.getText().toString();
txb1= Double.parseDouble(tb1);
txb2= Double.parseDouble(tb2);
txb3= Double.parseDouble(tb3);
txb4= Double.parseDouble(tb4);
txb5= Double.parseDouble(tb5);
}
You are getting NumberFormatException because you are parsing empty string.
You can use following method to safely parse double or set its value to some default if string does not contain valid double value.
public double getDoubleOrDefault(String value, double defValue)
{
try
{
return Double.parseDouble(value);
}
catch (Exception ex)
{
return defValue;
}
}
txb1 = getDoubleOrDefault(tb1, 0.0);
Double.parseDouble documentation