Question

Why is my app crashing? Following is an app that takes notes from users and shows...

Why is my app crashing?

Following is an app that takes notes from users and shows it's encrypted string. It also decrypt and encrypted strings. It saves the note in a file and also loads it back. Please help me identify what I am doing wrong?

activity_krypto_note.xml

<?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=".KryptoNoteActivity">

    <EditText
        android:id="@+id/key"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Cryptographic Key"
        android:inputType="number"
        app:layout_constraintEnd_toStartOf="@+id/encrypt"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/encrypt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:onClick="enryptClicked"
        android:text="ENCRYPT"
        android:textSize="9sp"
        app:layout_constraintEnd_toStartOf="@+id/decrypt"
        app:layout_constraintStart_toEndOf="@+id/key"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/decrypt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:onClick="decryptClicked"
        android:text="DECRYPT"
        android:textSize="9sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/encrypt"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/file"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="File Name"
        android:inputType="textCapWords"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/key" />

    <Button
        android:id="@+id/save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dp"
        android:layout_marginStart="1dp"
        android:onClick="saveClicked"
        android:text="SAVE"
        android:textSize="11sp"
        app:layout_constraintEnd_toStartOf="@+id/load"
        app:layout_constraintStart_toEndOf="@+id/file"
        app:layout_constraintTop_toBottomOf="@+id/encrypt" />

    <Button
        android:id="@+id/load"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="loadClicked"
        android:text="LOAD"
        android:textSize="11sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/save"
        app:layout_constraintTop_toBottomOf="@+id/decrypt" />

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/file">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:id="@+id/data"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="..."
                android:inputType="textCapWords"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="134dp" />
        </LinearLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

KryptoNoteActivity.java

package com.example.furquanhassan.kryptonote;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class KryptoNoteActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_krypto_note);
    }
        private String key = ((EditText) findViewById(R.id.key)).getText().toString();
        public static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        public  String makePad(String note)
        {
            StringBuilder pad = new StringBuilder(this.key);
            for (;pad.length()< note.length();)
            {
                pad.append(this.key);
            }
            return pad.toString();
        }

        public String encrypt(String note){
            String pad = makePad(note);
            StringBuilder result = new StringBuilder();
            for (int i = 0; i< note.length();i++)
            {
                String c = note.substring(i,i+1);
                int position = ALPHABET.indexOf(c);
                int shift = Integer.parseInt(pad.substring(i,i+1));
                int newPosition = (position+shift) % ALPHABET.length();
                result.append(ALPHABET.substring(newPosition, newPosition + 1));
            }
            return result.toString();
        }

        public String decrypt(String note)
        {
            String pad = makePad(note);
            StringBuilder result = new StringBuilder();
            for (int i = 0; i< note.length();i++)
            {
                String c = note.substring(i,i+1);
                int position = ALPHABET.indexOf(c);
                int shift = Integer.parseInt(pad.substring(i,i+1));
                int newPosition = (position-shift) % ALPHABET.length();
                result.append(ALPHABET.substring(newPosition, newPosition + 1));
            }
            return result.toString();
        }

    public void encryptClicked(View v){
        try {
            String note = ((EditText) findViewById(R.id.file)).getText().toString();
            String result = this.encrypt(note);
            ((EditText) findViewById(R.id.data)).setText(result);
        } catch (Exception e)
        {
            Toast label = Toast.makeText(this, e.getMessage(),Toast.LENGTH_SHORT);
            label.show();
        }
    }

    public void decryptClicked(View v)
    {
        try
        {
            String note = ((EditText) findViewById(R.id.file)).getText().toString();
            String result = this.decrypt(note);
            ((EditText) findViewById(R.id.data)).setText(result);
        } catch (Exception e)
        {
            Toast label = Toast.makeText(this, e.getMessage(),Toast.LENGTH_SHORT);
            label.show();
        }
    }


    public void saveClicked(View v)
    {
        try
        {
            String name = ((EditText) findViewById(R.id.file)).getText().toString();
            File dir = this.getFilesDir();
            File file = new File(dir,name);
            FileWriter fw = new FileWriter(file);
            fw.write(((EditText) findViewById(R.id.data)).getText().toString());
            fw.close();

        }catch (Exception e)
        {
           Toast label =  Toast.makeText(this,"Note Saved",Toast.LENGTH_LONG);
           label.show();
        }
    }

    public void loadClicked(View v)
    {
        try
        {
            String name = ((EditText) findViewById(R.id.file)).getText().toString();
            File dir = this.getFilesDir();
            File file = new File(dir,name);
            FileReader fr = new FileReader(file);
            StringBuilder show = new StringBuilder();
            for (int c = fr.read(); c!=-1;c=fr.read())
            {
                show.append((char) c);
            }
            fr.close();
            ((EditText) findViewById(R.id.data)).setText(show.toString());
        }catch (Exception e)
        {
            Toast label =  Toast.makeText(this,"File not found",Toast.LENGTH_LONG);
            label.show();
        }
    }
}

Use android studio if possible. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You have error in your project  because of spelling mistake of function name in file

 KryptoNoteActivity.java
 encryptClicked(View v)

does not match with spelling in xml file

 activity_krypto_note.xml
 android:onClick="enryptClicked" 

You need to keep function/method name same as defined in xml file

Error fixed code

activity_krypto_note.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".KryptoNoteActivity"> <EditText android:id="@+id/key" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:ems="10" android:hint="Cryptographic Key" android:inputType="number" app:layout_constraintEnd_toStartOf="@+id/encrypt" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/encrypt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:onClick="enryptClicked" android:text="ENCRYPT" android:textSize="9sp" app:layout_constraintEnd_toStartOf="@+id/decrypt" app:layout_constraintStart_toEndOf="@+id/key" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/decrypt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:onClick="decryptClicked" android:text="DECRYPT" android:textSize="9sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/encrypt" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/file" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:ems="10" android:hint="File Name" android:inputType="textCapWords" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/key" /> <Button android:id="@+id/save" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="1dp" android:layout_marginLeft="1dp" android:onClick="saveClicked" android:text="SAVE" android:textSize="11sp" app:layout_constraintEnd_toStartOf="@+id/load" app:layout_constraintStart_toEndOf="@+id/file" app:layout_constraintTop_toBottomOf="@+id/encrypt" /> <Button android:id="@+id/load" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:onClick="loadClicked" android:text="LOAD" android:textSize="11sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/save" app:layout_constraintTop_toBottomOf="@+id/decrypt" /> <ScrollView android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/file"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/data" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="..." android:inputType="textCapWords" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="134dp" /> </LinearLayout> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout>

KryptoNoteActivity.java

package com.example.furquanhassan.kryptonote; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public class KryptoNoteActivity extends AppCompatActivity { private String key; public static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_krypto_note); //key should be taken after view is loaded key= ((EditText) findViewById(R.id.key)).getText().toString(); } public String makePad(String note) { StringBuilder pad = new StringBuilder(this.key); for (; pad.length() < note.length(); ) { pad.append(this.key); } return pad.toString(); } public String encrypt(String note) { String pad = makePad(note); StringBuilder result = new StringBuilder(); for (int i = 0; i < note.length(); i++) { String c = note.substring(i, i + 1); int position = ALPHABET.indexOf(c); int shift = Integer.parseInt(pad.substring(i, i + 1)); int newPosition = (position + shift) % ALPHABET.length(); result.append(ALPHABET.substring(newPosition, newPosition + 1)); } return result.toString(); } public String decrypt(String note) { String pad = makePad(note); StringBuilder result = new StringBuilder(); for (int i = 0; i < note.length(); i++) { String c = note.substring(i, i + 1); int position = ALPHABET.indexOf(c); int shift = Integer.parseInt(pad.substring(i, i + 1)); int newPosition = (position - shift) % ALPHABET.length(); result.append(ALPHABET.substring(newPosition, newPosition + 1)); } return result.toString(); } public void enryptClicked(View v) { try { String note = ((EditText) findViewById(R.id.file)).getText().toString(); String result = this.encrypt(note); ((EditText) findViewById(R.id.data)).setText(result); } catch (Exception e) { Toast label = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT); label.show(); } } public void decryptClicked(View v) { try { String note = ((EditText) findViewById(R.id.file)).getText().toString(); String result = this.decrypt(note); ((EditText) findViewById(R.id.data)).setText(result); } catch (Exception e) { Toast label = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT); label.show(); } } public void saveClicked(View v) { try { String name = ((EditText) findViewById(R.id.file)).getText().toString(); File dir = this.getFilesDir(); File file = new File(dir, name); FileWriter fw = new FileWriter(file); fw.write(((EditText) findViewById(R.id.data)).getText().toString()); fw.close(); } catch (Exception e) { Toast label = Toast.makeText(this, "Note Saved", Toast.LENGTH_LONG); label.show(); } } public void loadClicked(View v) { try { String name = ((EditText) findViewById(R.id.file)).getText().toString(); File dir = this.getFilesDir(); File file = new File(dir, name); FileReader fr = new FileReader(file); StringBuilder show = new StringBuilder(); for (int c = fr.read(); c != -1; c = fr.read()) { show.append((char) c); } fr.close(); ((EditText) findViewById(R.id.data)).setText(show.toString()); } catch (Exception e) { Toast label = Toast.makeText(this, "File not found", Toast.LENGTH_LONG); label.show(); } } }

Output

Add a comment
Know the answer?
Add Answer to:
Why is my app crashing? Following is an app that takes notes from users and shows...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help to edit the code below. Am using it play a lottery game in Android...

    Need help to edit the code below. Am using it play a lottery game in Android Studio what i need help with is if play one game u need to clear it up before u could another game. 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" tools:context="simpleapps.my.lotteryapplication.MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/>...

  • Need help to edit the code below. Am using it play a lottery game in Android...

    Need help to edit the code below. Am using it play a lottery game in Android Studio what i need help with is if play one game u need to clear it up before u could another game. 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" tools:context="simpleapps.my.lotteryapplication.MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/>...

  • Need a help with this app. when I run this app, it is stopped. Here a...

    Need a help with this app. when I run this app, it is stopped. Here a source code for Tip calculator application. 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"> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFF" android:id="@+id/tableLayout" android:stretchColumns="1,2,3" android:padding="5dp"> <!-- tableRow0 --> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableRow0"> <TextView android:id="@+id/billTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/billTotal" android:textColor="#000" android:gravity="right" android:paddingRight="5dp"> </TextView> <EditText android:layout_width="wrap_content" android:id="@+id/billEditText" android:layout_height="wrap_content" android:layout_span="3" android:inputType="numberDecimal" android:layout_weight="1"> </EditText> </TableRow> <!-- tableRow1 --> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableRow1"> <TextView android:id="@+id/tenTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10%" android:textColor="#000" android:layout_column="1"...

  • Need help to build a fingerprint app that we have sslserversocket to tell a file use finger print...

    need help to build a fingerprint app that we have sslserversocket to tell a file use finger print before open the file what have been done below Here is my mainactivity.java and fingerprintHandler.java below <?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"> <TextView android:id="@+id/heading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:padding="20dp" android:text="Fingerprint Authentication" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="18sp" 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.0" /> <TextView android:id="@+id/paraMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="Place your finger on the fingerprint scanner to...

  • If anyone here is a Java and Android Studio expert please I really need help fulfilling...

    If anyone here is a Java and Android Studio expert please I really need help fulfilling the requirements needed to make a Rock Paper Scissors game. I mainly need Java code to complete it I will post what I have so far I don't have much time left please if anyone can help me I would really appreciate it. Here's what the app is supposed to do... The player should be able to select either Rock, Paper, or Scissors.The app...

  • Exercise 11-4 Create a Reminder app In this exercise, you’ll add a service to a Reminder...

    Exercise 11-4 Create a Reminder app In this exercise, you’ll add a service to a Reminder app that displays a notification every hour that says, “Look into the distance. It’s good for your eyes.” Test the app 1. Start Android Studio and open the project named ch11_ex4_Reminder. 2. Review the code. Note that it contains a layout and a class for an activity, but no class for a service. 1. Run the app. Note that it displays a message that...

  • Input hello, I need help completing my assignment for java,Use the following test data for input...

    Input hello, I need help completing my assignment for java,Use the following test data for input and fill in missing code, and please screenshot output. 1, John Adam, 3, 93, 91, 100, Letter 2, Raymond Woo, 3, 65, 68, 63, Letter 3, Rick Smith, 3, 50, 58, 53, Letter 4, Ray Bartlett, 3, 62, 64, 69, Letter 5, Mary Russell, 3, 93, 90, 98, Letter 6, Andy Wong, 3, 89,88,84, Letter 7, Jay Russell, 3, 71,73,78, Letter 8, Jimmie Wong,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT