-1

I am developing an application which decode's a string of characters that i put in the text field.

Then I press the button to decode the string of characters

I have build the application and then when i open it with my phone and when i open the application and put some caractere in the EditText and then i push decode the application close

this is my code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="decoder"
    android:id="@+id/button1"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

 </RelativeLayout>

MainActivity.java

package com.ghassensoussi7.decoder;

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


public class MainActivity extends AppCompatActivity  {

    private Button button1;
    private EditText editText1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button1 = (Button) findViewById(R.id.button1);
        editText1  = (EditText)  findViewById(R.id.editText);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String [] strings = 
  editText1.getText().toString().split("/");




                String s1 = strings[0];
                String s2 = strings[1];
                int s3 = Integer.parseInt(strings[2]);
                String s4 = s2 + s3;
                String s5 = strings[3];

                //1ere caractere   c'est une lettre

                if(s1.equals("a") || s1.equals("b") || s1.equals("c")) {
                    Toast.makeText(MainActivity.this, "tu appartient a la 
  classe 1-1 ", Toast.LENGTH_LONG).show();
                }
                else
                    if (s1.equals("d") || s1.equals("e") || s1.equals("f")) 
 {
                        Toast.makeText(MainActivity.this, "tu appartient a 
 la classe 1-2 ", Toast.LENGTH_LONG).show();
                    }
                else
                    if (s1.equals("g") || s1.equals("h") || s1.equals("i")) 
 {
                        Toast.makeText(MainActivity.this, "tu appartient a 
 la classe 1-3 ", Toast.LENGTH_LONG).show();
                    }

                //2eme caractere  c'est une lettre

            if(s2.equals("a") || s2.equals("b") || s2.equals("c")) {
                Toast.makeText(MainActivity.this, "tu appartient a la classe 
2-1 ", Toast.LENGTH_LONG).show();
            }
                else

            if(s2.equals("d") || s2.equals("e") || s2.equals("f")) {
                Toast.makeText(MainActivity.this, "tu appartient a la classe 
2-2 ", Toast.LENGTH_LONG).show();
            }

                //3eme caractere c'est un chiffre

                switch (s3)
                {

                    case 1:
                        Toast.makeText(MainActivity.this, "numero inferieur a la moyen", Toast.LENGTH_LONG).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this, "numero egal a la moyen", Toast.LENGTH_LONG).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this, "numero superieur a la moyen", Toast.LENGTH_LONG).show();
                        break;

                }


                //4eme et 5eme caractere


                if(s4.equals("aa") || s2.equals("bb") || s2.equals("cc")) {
                    Toast.makeText(MainActivity.this, "tu appartient a la classe XX ", Toast.LENGTH_LONG).show();
                }

                else
                    if (s4.equals("dd") || s2.equals("ee") || s2.equals("ff")) {
                        Toast.makeText(MainActivity.this, "tu appartient a la classe YY ", Toast.LENGTH_LONG).show();
                    }
                else
                    if (s4.equals("gg") || s2.equals("hh") || s2.equals("ii")) {
                        Toast.makeText(MainActivity.this, "tu appartient a la classe ZZ ", Toast.LENGTH_LONG).show();
                    }
    }

        });
    }
}

I have build the application and then when i open it with my phone and when i open the application and put some caractere in the EditText and then i push decode the application close

This is my code. I need help and thanks

GHASSEN7
  • 9
  • 4
  • Event Log show : Null PointerException : Error executing task.com.android.tools.idea.uibuilder.editor.NLPreviewForm$$Lambda$221/13822358828@4d5062ca NullPointerException : null – GHASSEN7 Apr 26 '17 at 17:04

1 Answers1

0

In the code you posted you set an onClickListener() on button1 but you declare the button in the onClickListener. The rest seems ok. If you look on the Android Monitor, you see the exact error log. Would you be so kind to post it?

Edit:

Put the declaration of button1 before the OnClickListener().

Dominik 105
  • 64
  • 1
  • 8
  • Null PointerException : Error executing task.com.android.tools.idea.uibuilder.editor.NLPreviewForm$$Lambda$221/13822358828@4d5062ca NullPointerException : null – GHASSEN7 Apr 26 '17 at 17:08
  • Dominik what should i do ? – GHASSEN7 Apr 26 '17 at 17:09
  • you get a NullPointerException since you put your declaration of `button1` into the OnClickListener(). Put it before you set an OnClickListener and your code will hopefully work. – Dominik 105 Apr 26 '17 at 17:12
  • Yes it work thanx !!! but now when i put some caractère in the EditText and then I push the button decode the application close – GHASSEN7 Apr 26 '17 at 17:34
  • You do not have `editText1` initialized. The same problem as before. Initialize it like `button1` with `findViewById` before your Listener and it will work. Otherwise please post the complete StackTrace, there you can see the exact line, where the error occurs. – Dominik 105 Apr 26 '17 at 17:44
  • i initialize it : editText1 = (EditText) findViewById(R.id.editText); but the problème does not fixed the application close when i push the decode button – GHASSEN7 Apr 26 '17 at 17:55
  • Would you post your complete StackTrace? Makes the search for an error much easier. – Dominik 105 Apr 26 '17 at 18:02
  • Dominik you mean i copy the new code and past it ? i have already change the code and post the new code – GHASSEN7 Apr 26 '17 at 18:31
  • I mean the error you get shown when your application crashes. You can find it when you click in Android Studio on Android Monitor on the bottom. There are you find all logs of your app and the errors are shown with full stack there. It also shows you the line number where the error occurs in your app. – Dominik 105 Apr 26 '17 at 18:35
  • ok i will post it but give me time because first i have work with the computer of my friend to get the message error but now my friend has left so i must install my device mobile on android studio so that i can run it and see the error message and thanx a lot foy your help – GHASSEN7 Apr 26 '17 at 18:40
  • Domminik i have take much time to install my mobile device with android studio this is what the error message show : E/AndroidRuntime: FATAL EXCEPTION: main – GHASSEN7 Apr 27 '17 at 19:07
  • Sorry Ghassen, but I cannot help you. This is just the first line of the stacktrace, it means your app has crashed and this appears at almost every error. If you need help, please look up in your stacktrace. Most likely you have a bit down a `caused by` where you see the exact error. Put this into google and see what you find. At the stacktrace you have links to all the lines affected by the error and the links to the lines in your code are highlighted. Check through your code and click on the links in your stacktrace and you will find the exact line where your error occurs. – Dominik 105 Apr 28 '17 at 20:42
  • thanx dominik i have solve the probleme – GHASSEN7 Apr 28 '17 at 22:25
  • but i want to change the method of display – GHASSEN7 Apr 28 '17 at 22:26
  • i want a methode that shows all the messages together and in the same window – GHASSEN7 Apr 28 '17 at 22:26
  • can you help me to transform the code , i think to display the messages into EditTexts ? – GHASSEN7 Apr 28 '17 at 22:27
  • Sorry, Stackoverflow is not the right place for this. Take a Android course on any platform you like to learn programming in Android. – Dominik 105 Apr 29 '17 at 17:25