The problem with Android Studio Java code: com.example.myapplication.mainActivity.onCreate

huangapple go评论68阅读模式
英文:

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

问题

It seems like you're encountering an issue with your Android app, and you've provided code and error messages. However, you haven't specified the exact problem you're facing or what you'd like assistance with. If you have a specific question or need help with a particular aspect of your code or the error message you're receiving, please let me know, and I'll do my best to assist you.

英文:

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

答案1

得分: 1

您正在尝试访问未初始化的gender RadioGroup,并且代码中存在其他问题,变量clearenter未初始化。在gender.setOnCheckedChangeListener()方法中,您尝试查找一个RadioButton,并且在尝试初始化任何视图之前,您需要调用setContentView()。您的代码应该如下所示:

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

    gender = findViewById(R.id.gender);
    Male = findViewById(R.id.Male);
    Female = findViewById(R.id.Female);
    create = findViewById(R.id.Create);
    login = findViewById(R.id.Login);
    clear = findViewById(R.id.clear);
    enter = findViewById(R.id.enter);

    gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
            RadioButton radioButton = findViewById(checkedId);
            //....
        }
    });

    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            gender.clearCheck();
        }
    });

    enter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //获取选中的RadioButton
            //....
        }
    });

    //....
}
英文:

You are trying to access the gender RadioGroup without initializing it first and there are some other issues in your code, variables clear and enter are not initialized, in the gender.setOnCheckedChangeListener() method, you are trying to find a RadioButton and you need to setContentView() before trying to initialize any of the views. Your code should be something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_file_name);
gender = findViewById(R.id.gender);
Male = findViewById(R.id.Male);
Female = findViewById(R.id.Female);
create = findViewById(R.id.Create);
login = findViewById(R.id.Login);
clear = findViewById(R.id.clear);
enter = findViewById(R.id.enter);
gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
RadioButton radioButton = findViewById(checkedId);
//....
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gender.clearCheck();
}
});
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Get the selected RadioButton
//....
}
});
//....
}

huangapple
  • 本文由 发表于 2023年4月17日 11:07:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定