Android Studio Program Crash - java

I have two activities Main, and Game_Activity
When my app launches and I click a button to take me to the Game_Activity it crashes...
Main
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHelp = (Button) findViewById(R.id.btnHelp);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
btnStart.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, GameActivity.class); //gets us to Game activity when Start Button is clicked
startActivity(i);
overridePendingTransition(R.animator.animation1, R.animator.animation2);
}
});
}
Game_Activity
ViewGroup kamilsLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kamilsLayout = (ViewGroup) findViewById(R.id.kamilsLayout);
Button kamilsButton2 = (Button) findViewById(R.id.kamilsButton2);
kamilsButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
moveButton();
}
});
}
public void moveButton(){
View kamilsButton = findViewById(R.id.kamilsButton);
TransitionManager.beginDelayedTransition(kamilsLayout);
//Change size of the button
ViewGroup.LayoutParams sizeRules = kamilsButton.getLayoutParams();
sizeRules.height = 700;
kamilsButton.setLayoutParams(sizeRules);
}
Here is Logcat sorry for last lines but couldn't format it... :/
03-02 21:32:49.073 22427-22427/com.example.kamil.mychemicalreaction E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kamil.mychemicalreaction, PID: 22427
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kamil.mychemicalreaction/com.example.kamil.mychemicalreaction.GameActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.kamil.mychemicalreaction.GameActivity.onCreate(GameActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)at android.app.ActivityThread.access$900(ActivityThread.java:161)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:126)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:157)at android.app.ActivityThread.main(ActivityThread.java:5356)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)at dalvik.system.NativeStart.main(Native Method)
XML
Game_Activity
<Button
android:layout_width="150dp"
android:layout_height="1dp"
android:id="#+id/kamilsButton"
android:gravity="bottom|fill_vertical"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#ff6aff01" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Click"
android:id="#+id/kamilsButton2"
android:textSize="9dp"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="102dp" />
Menu
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Chemical Reaction"
android:id="#+id/txtTitle"
android:textStyle="bold"
android:textColor="#ffa946ff"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btnStart"
android:text="Start"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginBottom="61dp"
android:layout_above="#+id/btnSettings"
android:layout_alignLeft="#+id/btnSettings"
android:layout_alignStart="#+id/btnSettings" />
<Button
android:id="#+id/btnSettings"
android:text="Settings"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_above="#+id/btnHelp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp" />
<Button
android:id="#+id/btnHelp"
android:text="Help"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/btnSettings"
android:layout_alignStart="#+id/btnSettings"
android:layout_marginBottom="81dp" />

You're getting a NullPointerException when trying to reference your UI elements. From your XML, it appears you haven't given any layout an id of kamilsLayout. Try enclosing your UI elements in a layout with this ID.

Like Josef said. You're setting the same XML for two both activities, is that expected?
You're using setContentView(R.layout.activity_main). Maybe there is no ViewGroup on R.layout.activity_main layout. And you're trying to setting up that `
kamilsLayout = (ViewGroup) findViewById(R.id.kamilsLayout);
`

Related

E/AndroidRuntime: FATAL EXCEPTION: main in android studio

While I try to run it shows the app has stopped and I got the following error in the logcat:
12-18 21:47:44.545 26245-26245/com.example.bloodpressuremonitoring1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bloodpressuremonitoring1, PID: 26245
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bloodpressuremonitoring1/com.example.bloodpressuremonitoring1.MainActivity}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.ImageView
at com.example.bloodpressuremonitoring1.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
Here is my MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static int SPLASH_SCREEN = 5000;
//Variables
Animation topAnim, bottomAnim;
ImageView image;
TextView logo, slogan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//Animations
topAnim = AnimationUtils.loadAnimation(this, R.anim.top_animation);
bottomAnim = AnimationUtils.loadAnimation(this, R.anim.bottom_animation);
//Hooks
image = findViewById(R.id.imageView);
logo = findViewById(R.id.textView);
slogan = findViewById(R.id.textView2);
image.setAnimation(topAnim);
logo.setAnimation(bottomAnim);
slogan.setAnimation(bottomAnim);
}
}
Here is my activity_main.xml:
<ImageView
android:layout_width="266dp"
android:layout_height="312dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/blood_pressure"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:fontFamily="#font/bilbo_swash_caps"
android:gravity="center"
android:text="Blood Pressure Monitoring"
android:textSize="35sp"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:fontFamily="sans-serif-smallcaps"
android:gravity="center"
android:text="Monitor your blood pressure anytime"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Please help me, thanks a lot...
Your ImageView doesn't have an id
<ImageView
android:id="#+id/imageView"
...
/>
Maybe you put #+id/imageView on the ConstraintLayout by accident?
You have accidently added imageview id to Constraintlayout id. just change that it will work fine.
Error:
androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.ImageView

Simple android studio app is crashing when i run it. it have no errors

i made a simple betting app for me and my friend.
I press one button from the bottom to set the value of the bid. and after that if i win i press i win and otherwise i press i lost. when i press i lost or i win the text view get the value of the bet. I dont have any errors , i dont know why is it crashing when i open the app.
here is the code:
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int totalMoney= 0;
int betValue = 0;
String savedMoney;
TextView textView = (TextView) findViewById(R.id.textView);
public void clickP(View view){
totalMoney= totalMoney - betValue;
}
public void clickC(View view){
totalMoney= totalMoney + betValue;
}
public void click1(View view){
betValue = 1;
}
public void click2(View view){
betValue = 2;
}
public void click3(View view){
betValue = 3;
}
public void click4(View view){
betValue = 4;
}
public void click5(View view){
betValue = 5;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savedMoney = textView.getText().toString();
totalMoney = Integer.parseInt(savedbani);
SharedPreferences sharedPreferences = this.getSharedPreferences("com.markusappcompany.baniipemasa", Context.MODE_PRIVATE);
sharedPreferences.edit().putInt("Money", totalMoney).apply();
sharedPreferences.getInt("Money", totalMoney);
textView.setText(totalMoney);
}
}
This is what i get in Logcat:
05-22 19:54:52.918 20536-20536/com.markusappcompany.baniipemasa E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.markusappcompany.baniipemasa, PID: 20536
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.markusappcompany.baniipemasa/com.markusappcompany.baniipemasa.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:338)
at android.widget.TextView.setText(TextView.java:5494)
at com.markusappcompany.baniipemasa.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
This is my 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="com.markusappcompany.baniipemasa.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="117dp"
android:layout_marginEnd="1dp"
android:layout_marginStart="80dp"
android:layout_marginTop="228dp"
android:text="I LOST"
app:layout_constraintBottom_toTopOf="#+id/button3"
app:layout_constraintEnd_toStartOf="#+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="117dp"
android:layout_marginEnd="80dp"
android:layout_marginStart="1dp"
android:layout_marginTop="228dp"
android:text="I WIN"
app:layout_constraintBottom_toTopOf="#+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginBottom="70dp"
android:layout_marginStart="16dp"
android:text="1 EUR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button4"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:layout_marginTop="117dp"
android:text="2 EUR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button5"
app:layout_constraintStart_toEndOf="#+id/button3"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:layout_marginTop="117dp"
android:text="3 EUR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button6"
app:layout_constraintStart_toEndOf="#+id/button4"
app:layout_constraintTop_toBottomOf="#+id/button2" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="117dp"
android:text="4 LEI"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button5"
app:layout_constraintTop_toBottomOf="#+id/button2" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="148dp"
android:layout_marginStart="148dp"
android:text="5 LEI"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="163dp"
android:layout_marginEnd="221dp"
android:layout_marginStart="190dp"
android:layout_marginTop="16dp"
android:text="0"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.487"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="25dp"
android:text="Total money:"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="#+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
thanks in advance!
First of all you remove all click methods your activity should look like this:
public class MainActivity extends AppCompatActivity {
int totalMoney= 0;
int betValue = 0;
String savedMoney;
TextView textView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
Button yourButton = findViewById(R.layout.yourButtonId)
Button yourButton2 = findViewById(R.layout.yourButtonId2)
// for all Buttons
}}
You also must give the button an id in the activity_main file. After that you have to implement an onClickListener for all thes buttons like this:
yourButton1.setOnClickListener(this);
// the same for every Button
then you add this line:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
at the end the listener:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.yourButtonID:
// when button with this ID is clicked do your stuff here
case R.id.otherButtonID:
// when button with this ID is clicked do your stuff here
}
}
Here the explanation first you find the buttons and the textView after that you set an onclicklistener so when you click one button the button call the method onClick. The switch statment is called an finds out which button is called.
Place your Textview's findTextViewById inside OnCreateView.
Try to find the Buttons and textViews in the onCreate method.
Also try to handle the Buttons with OnClickListener like this:
Button yourButtton = findViewById(R.id.yourButttonID);
yourButtton.setOnClickListener(this);
And this for all Buttons.
then:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.yourButtonID:
// when button with this ID is clicked
case R.id.otherButtonID:
// when button with this ID is clicked
}
}

Button closing the app instead of switching activity

I am trying to make a game Tic-Tac-Toe using android studio. When I try to switch from MainActivity to another activity(Game), instead of switching, the button closes the app. I have added several buttons in Game activity but have not used them and android studio is not showing any error about that so is the problem in Game activity or MainActivity? And how to solve this problem.
Main Activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play = (Button)findViewById(R.id.play);
Button quit = (Button)findViewById(R.id.quit);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGame();
}
});
quit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
quitGame();
}
});
}
void openGame() {
Intent intent = new Intent(MainActivity.this, Game.class);
startActivity(intent);
}
void quitGame() {
finish();
System.exit(0);
}
}
Game Activity:
public class Game extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.bg);
constraintLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
String color1[] = {"#fb5959", "#fe7738", "#f3980b", "#DBD100", "#A8D700", "#87E50C"};
String color2[] = {"#54F370", "#00FFBC", "#00FFFF", "#00FFE9", "#00C8FF", "#0085FF"};
Random random = new Random();
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BL_TR,
new int[]{Color.parseColor(color1[random.nextInt(6)]),Color.parseColor(color2[random.nextInt(6)])});
constraintLayout.setBackgroundDrawable(gradientDrawable);
return false;
}
});
Button one = (Button) findViewById(R.id.one);
Button two = (Button) findViewById(R.id.two);
Button three = (Button) findViewById(R.id.three);
Button four = (Button) findViewById(R.id.four);
Button five = (Button) findViewById(R.id.five);
Button six = (Button) findViewById(R.id.six);
Button seven = (Button) findViewById(R.id.seven);
Button eight = (Button) findViewById(R.id.eight);
Button nine = (Button) findViewById(R.id.nine);
}
}
Stack Trace:
05/05 13:14:41: Launching 'app' on OnePlus ONE A2003.
$ adb shell am start -n "com.example.tictactoe/com.example.tictactoe.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 21369 on device 'oneplus-one_a2003-3222ee10'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Choreographer: Skipped 107 frames! The application may be doing too much work on its main thread.
E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
D/AppTracker: App Event: start
E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
D/AppTracker: App Event: stop
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tictactoe, PID: 21369
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tictactoe/com.example.tictactoe.Game}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.tictactoe.Game.onCreate(Game.java:20)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
at android.app.ActivityThread.access$900(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5497) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class Button
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 
at com.example.tictactoe.Game.onCreate(Game.java:20) 
at android.app.Activity.performCreate(Activity.java:6289) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
at android.app.ActivityThread.access$900(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5497) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x10101e6 a=-1}
at android.content.res.TypedArray.getDrawable(TypedArray.java:867)
at android.view.View.<init>(View.java:3964)
at android.widget.TextView.<init>(TextView.java:680)
at android.widget.Button.<init>(Button.java:109)
at android.widget.Button.<init>(Button.java:105)
at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:72)
at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:68)
at androidx.appcompat.app.AppCompatViewInflater.createButton(AppCompatViewInflater.java:192)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:111)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 
at com.example.tictactoe.Game.onCreate(Game.java:20) 
at android.app.Activity.performCreate(Activity.java:6289) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
at android.app.ActivityThread.access$900(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5497) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
D/AppTracker: App Event: crash
I/Process: Sending signal. PID: 21369 SIG: 9
Game Activity Xml Code:
<?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:id="#+id/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0D95DF"
tools:context=".Game">
<Button
android:id="#+id/one"
android:layout_width="91dp"
android:layout_height="90dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.015"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.029" />
<Button
android:id="#+id/two"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.034" />
<Button
android:id="#+id/three"
android:layout_width="91dp"
android:layout_height="90dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.984"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.034" />
<Button
android:id="#+id/four"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.015"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.497" />
<Button
android:id="#+id/five"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.497" />
<Button
android:id="#+id/six"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.984"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.497" />
<Button
android:id="#+id/seven"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.015"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.975" />
<Button
android:id="#+id/eight"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.975" />
<Button
android:id="#+id/nine"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.984"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.975" />
<TextView
android:id="#+id/player1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 1 (X) :"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.177"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.098" />
<EditText
android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toEndOf="#+id/player1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.088" />
<TextView
android:id="#+id/player2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 2 (O) :"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.175"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.177" />
<EditText
android:id="#+id/name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toEndOf="#+id/player1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="20dp"
app:layout_constraintGuide_percent="0.3" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="462dp"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp"
app:layout_constraintGuide_percent="0.15" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="358dp"
app:layout_constraintGuide_percent="0.85" />
<View
android:id="#+id/vertical1"
android:layout_width="4dp"
android:layout_height="282dp"
android:background="?android:attr/listDivider"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.5"
tools:visibility="visible" />
<View
android:id="#+id/vertical2"
android:layout_width="4dp"
android:layout_height="282dp"
android:background="?android:attr/listDivider"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.5"
tools:visibility="visible" />
<View
android:id="#+id/horizontal1"
android:layout_width="282dp"
android:layout_height="4dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.333" />
<View
android:id="#+id/horizontal2"
android:layout_width="282dp"
android:layout_height="4dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.666" />
</androidx.constraintlayout.widget.ConstraintLayout>
what you have is an inflation exception, Android is unable to inflate Button because it does not recognize an ?attr/... i had a similar problem a while back. Try removing this line in xml.
android:background="?android:attr/selectable"
if problem persists remove all lines with.
android:.......="?attr/...."

Handling Radio Group and Button in Fragment

first take a look on a part of my fragment xml file `
<LinearLayout
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="#+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rbclick"
android:text="0" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="1" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="4" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="5" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:onClick="rbclick"
android:text="6" />
</RadioGroup>
</LinearLayout>
now take look on my fragment java class
public class MealEntry extends Fragment {
private MealEntryViewModel mViewModel;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private static final String TAG = "MealEntry";
public RadioGroup rg;
RadioButton rb;
public static MealEntry newInstance() {
return new MealEntry();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
return view;
}
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb=(RadioButton)findViewById(radiobtnid);
}
previously by this procedure i mean the (rbclick function ) can handle the radio group and radio button in Activity(i repeat Activity). but in Fragment the onClick method is not working. how can i resolve this? I've tried this way for fragment.
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)getActivity().findViewById(radiobtnid);
}
I think this handler is not working. cause it's got crash when i click the radio button.
here's is Log cat.
12-18 22:29:07.715 3720-3720/com.example.closeup W/PathParser: Points are too far apart 4.000000596046461
12-18 22:29:11.963 3720-3720/com.example.closeup W/ResourceType: No package identifier when getting name for resource number 0x00000016
12-18 22:29:11.964 3720-3720/com.example.closeup D/AndroidRuntime: Shutting down VM
12-18 22:29:11.972 3720-3720/com.example.closeup E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.closeup, PID: 3720
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x16
at android.content.res.Resources.getResourceEntryName(Resources.java:2127)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:433)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
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)
12-18 22:34:12.034 3720-3720/com.example.closeup I/Process: Sending signal. PID: 3720 SIG: 9
Here is solution to handle radio group in fragment.
1.Remove all line android:onClick="rbclick" from each RadioButton in meal_entry_fragment.xml file, and create an id for each RadioButton.
<?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="wrap_content"
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="1" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="3" />
<RadioButton
android:id="#+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="4" />
<RadioButton
android:id="#+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="5" />
<RadioButton
android:id="#+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="6" />
</RadioGroup>
</LinearLayout>
2.Create an instance of OnCheckedChangeListener in MealFragment class.
private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio0:
// Write your code here
break;
case R.id.radio1:
// Write your code here
break;
case R.id.radio2:
// Write your code here
break;
case R.id.radio3:
// Write your code here
break;
case R.id.radio4:
// Write your code here
break;
case R.id.radio5:
// Write your code here
break;
case R.id.radio6:
// Write your code here
break;
default:
break;
}
}
};
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
rg.setOnCheckedChangeListener(onCheckedChangeListener);
return view;
}
Argument View v in public void rbclick(View v) is the RadioButton you are looking for
Try using this code below:
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)v.findViewById(radiobtnid);
}

Adding an items on layout X and Y and to summarized all in other activity

I just want to ask you how to add an item from my layout x and y, and how to summarize them all.
My main goal why I posted here is to add all items from different layout in one layout, Shopping List (By the way, I'm currently developing a mobile grocery app)
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3724)
at android.view.View.performClick(View.java:4261)
at android.view.View$PerformClick.run(View.java:17356)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3719)
            at android.view.View.performClick(View.java:4261)
            at android.view.View$PerformClick.run(View.java:17356)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.admin.mobile_grocery.Baby_Food.ocaddtocart(Baby_Food.java:79)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3719)
            at android.view.View.performClick(View.java:4261)
            at android.view.View$PerformClick.run(View.java:17356)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
Baby Diaper (Java) as Layout X
public class Baby_Diaper extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoCart;
Intent i = new Intent(this, Shopping_List.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baby__diaper);
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_diaper);
}
public void SelectItem (View view) {
boolean checked = ((CheckBox) view) .isChecked();
switch (view.getId())
{
case R.id.pampers:
if(checked)
{selection.add("Pampers");}
else
{
selection.remove ("Pampers");
}
break;
case R.id.huggies:
if(checked)
{selection.add("Huggies");}
else
{
selection.remove ("Huggies");
}
break;
case R.id.johnsons:
if(checked)
{selection.add("Johnsons");}
else
{
selection.remove ("Johnsons");
}
break;
case R.id.supreme:
if(checked)
{selection.add("Supreme");}
else
{
selection.remove ("Supreme");
}
break;
}
}
public void ocaddtocart(View view){
String final_shopping_selection = "";
for (String Selections : selection){
final_shopping_selection = final_shopping_selection + Selections + "\n";
}
final_text.setText(final_shopping_selection);
final_text.setEnabled(true);}
public void ocgtshoppinglist (View view){
Intent x = new Intent(Baby_Diaper.this, Shopping_List.class);
x.putExtra("items", final_text.getText().toString());
startActivity(x);
}
Baby Food (Java) as layout Y
public class Baby_Food extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoCart;
Intent i = new Intent(this, Shopping_List.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
setContentView(R.layout.activity_baby__food);
}
public void SelectItem (View view) {
boolean checked = ((CheckBox) view) .isChecked();
switch (view.getId())
{
case R.id.coryandgate:
if(checked)
{selection.add("Cory & Gate");}
else
{
selection.remove ("Cory & Gate");
}
break;
case R.id.gerber:
if(checked)
{selection.add("Gerber");}
else
{
selection.remove ("Gerber");
}
break;
case R.id.hipp:
if(checked)
{selection.add("Hipp");}
else
{
selection.remove ("Hipp");
}
break;
}
}
public void ocaddtocart(View view){
String final_shopping_selection = "";
for (String Selections : selection){
final_shopping_selection = final_shopping_selection + Selections + "\n";
}
final_text.setText(final_shopping_selection);
final_text.setEnabled(true);
}
public void ocgtshoppinglist (View view){
Intent x = new Intent(Baby_Food.this, Shopping_List.class);
x.putExtra("items", final_text.getText().toString());
startActivity(x);
}
Shopping List (layout where can view all items)
public class Shopping_List extends ActionBarActivity {
TextView final_result_shopping;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping__list);
final_result_shopping= (TextView)findViewById(R.id.final_result_shopping);
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("items");
final_result_shopping.setText(newString);
}
} else {
newString= (String) savedInstanceState.getSerializable("items");
final_result_shopping.setText(newString);
}
}
Baby Diaper (Layout X) XML
<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"
tools:context="com.example.admin.mobile_grocery.Baby_Diaper"
android:id="#+id/baby_diaper">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pampers"
android:id="#+id/pampers"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:checked="false"
android:onClick="SelectItem"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/huggies"
android:id="#+id/huggies"
android:layout_below="#+id/pampers"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/johnsons"
android:id="#+id/johnsons"
android:layout_below="#+id/huggies"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
android:inputType="textNoSuggestions"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/supreme"
android:id="#+id/supreme"
android:layout_below="#+id/johnsons"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="SelectItem"
android:inputType="textNoSuggestions"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/addtocart"
android:id="#+id/addtocart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:inputType="textNoSuggestions"
android:onClick="ocaddtocart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello Shoppers!"
android:id="#+id/final_shopping_diaper"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO SHOPPING LIST"
android:id="#+id/gt_shopping_list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="ocgtshoppinglist"
/>
Baby Food (layout Y) XML
<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"
tools:context="com.example.admin.mobile_grocery.Baby_Food"
android:id="#+id/baby_food">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cory & Gate"
android:id="#+id/coryandgate"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:checked="false"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gerber"
android:id="#+id/gerber"
android:layout_below="#+id/coryandgate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hipp"
android:id="#+id/hipp"
android:layout_below="#+id/gerber"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD TO CART / REMOVE"
android:id="#+id/addtocart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="ocaddtocart"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello Shoppers!"
android:id="#+id/final_shopping_food"
android:layout_below="#+id/hipp"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO SHOPPING LIST"
android:id="#+id/gt_shopping_list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Shopping List (Receiver layout) XML
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.admin.mobile_grocery.Shopping_List"
android:id="#+id/shopping_list_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Shopping List"
android:id="#+id/shopping_list"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Shoppers!"
android:id="#+id/final_result_shopping"
android:layout_marginTop="33dp"
android:layout_below="#+id/reservation_list"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Hope some pro will lend their knowledge to help me in my problem.
Ok I found it.
Replace
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);
setContentView(R.layout.activity_baby__food);
With
setContentView(R.layout.activity_baby__food);
addtoCart = (Button) findViewById(R.id.addtocart);
final_text = (TextView)findViewById(R.id.final_shopping_food);

Categories

Resources