dimanche 16 avril 2023

The problem with android studio jave code com.example.myapplication.mainActivity.onCreate

The problem on i run app it come

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 13080
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.mainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        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.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.myapplication.mainActivity.onCreate(mainActivity.java:31)
        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) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

my java code is

package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
    @SuppressLint("SuspiciousIndentation")
    public class mainActivity extends AppCompatActivity {
        Button create, login, enter, clear;
        RadioGroup gender;
        RadioButton Male, Female;
        private String msg;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            gender = (RadioGroup) findViewById(R.id.gender);
            Male = (RadioButton) findViewById(R.id.Male);
            Female = (RadioButton) findViewById(R.id.Female);
            create = (Button) findViewById(R.id.Create);
            login = (Button) findViewById(R.id.Login);
            create = findViewById(R.id.Create);
            login = findViewById(R.id.Login);
            gender.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Get the selected RadioButton
                    int checkId = gender.getCheckedRadioButtonId();
                    // compare selected's Id with individual RadioButtons ID
                    if (checkId == -1) {
                            Message.message(getApplicationContext(), msg + "It's done");
                    } else {
                        findRaqdioButton(checkId);
                    }
                }
            });
            clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    gender.clearCheck();
                }
            });
            gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                    gender = findViewById(checkedId);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        Message.message(getApplicationContext(), gender.getTooltipText().toString());
                    }
                }
            });
        }
        private void findRaqdioButton(int checkId) {
            switch (checkId) {
                case R.id.Male:
                    Message.message(getApplicationContext(), msg + "Male");
                    break;
                case R.id.Female:
                    Message.message(getApplicationContext(), msg + "Female");
                    break;
            }
        }
    }

this xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/b1"
    tools:context=".mainActivity"
    tools:ignore="ExtraText">
    <TextView
        android:id="@+id/title_textview"
        android:text="Create your new account"
        android:textSize="30sp"
        android:textColor="#FFFFFF"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/smile_imageview"
        android:src="@mipmap/icons"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_height="wrap_content"
        android:id="@+id/firstname"
        android:layout_width="match_parent"
        android:text="Frist name"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#FFFFFF" />
    <EditText
        android:id="@+id/Name_edit"
        android:hint="@string/first_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:background="#FAF8F8" />
    <TextView
        android:layout_height="wrap_content"
        android:id="@+id/lastname"
        android:layout_width="match_parent"
        android:text="Last name"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#FFFFFF" />
    <EditText
        android:id="@+id/lastname_edit"
        android:hint="Last name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:background="#FAF8F8" />
    <TextView
        android:layout_height="wrap_content"
        android:id="@+id/Email"
        android:layout_width="match_parent"
        android:text="Email"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#FFFFFF" />
    <EditText
        android:id="@+id/Email_edit"
        android:hint="Enter your Email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:background="#FAF8F8" />
    <TextView
        android:layout_height="wrap_content"
        android:id="@+id/Passwoed"
        android:layout_width="match_parent"
        android:text="Password"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#FFFFFF" />
    <EditText
        android:id="@+id/Password_edit"
        android:hint="Enter your Password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:background="#FAF8F8" />
    <RadioGroup
        android:id="@+id/gender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <RadioButton
            android:id="@+id/Male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male"
            android:textColor="#FFFFFF"/>
        <RadioButton
            android:id="@+id/Female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/female"
            android:textColor="#FFFFFF"/>
    </RadioGroup>
   <TableLayout
    android:id="@+id/table1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*">

    <TableRow>
        <Button
            android:id="@+id/enter"
            android:text="choose"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/clear"
            android:text="@string/clear"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />
    </TableRow>
   </TableLayout>
    <CheckBox
        android:id="@+id/ch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I agree with Terms of Service"
        android:textStyle="bold"
        android:textColor="#FFFFFF" >
    </CheckBox>
    <CheckBox
        android:id="@+id/ch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=
            "@string/i_understand_that_if_i_loss_my_password_i_may_lose_my_data_read_more_information_on_help"
        android:textStyle="bold"
        android:textColor="#FFFFFF">
    </CheckBox>
    <TableLayout
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*">
    <TableRow>
    <Button
        android:id="@+id/Create"
        android:text="Create account"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/Login"
        android:text="Log in"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    </TableRow>
        <TableRow>
            <TextView
                android:text="Help"
                android:id="@+id/help"
                android:textColor="#FFFFFF"
                android:padding="5dp"/>
            <TextView
                android:text="How you can use it"
                android:id="@+id/how"
                android:textColor="#FFFFFF"
                android:padding="5dp"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

My app always stopped i can't run




Aucun commentaire:

Enregistrer un commentaire