Manan Adhvaryu Manan Adhvaryu - 27 days ago 8
Android Question

Trying to find factorial and display it in a new activity. Crash on startup

I'm trying to make this app where I'm taking a value from the main activity sending it to the second one where I generate it's factorial and display it. I'm probably doing something wrong, here is the code of the mainactivity class:

public class MainActivity extends AppCompatActivity implements
View.OnClickListener
{

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

EditText n = (EditText) findViewById(R.id.input_num);
Bundle bundle = new Bundle();

public void onClick(View v)
{
switch (v.getId())
{
case R.id.ok_button:
int val = Integer.parseInt(n.getText().toString());
bundle.putInt("nval",val);
Intent intent = new Intent(this, Answer.class);
intent.putExtras(bundle);
startActivity(intent);
break;

}
}
}


This is the second class:

public class Answer extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
TextView textView = (TextView) findViewById(R.id.answer);
int val = bundle.getInt("nval");
int x = 1;
for(int i=1; i<val; i++)
{
x =(x*i);
}

String txt = Integer.toString(x);
textView.setText(txt);
}


XML for main activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.manan.fibbo.MainActivity">

<EditText
android:id="@+id/input_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginEnd="120dp"
android:layout_marginTop="180dp"
android:hint="@string/fibnum"
/>
<Button
android:id="@+id/ok_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="120dp"
android:text="@string/but_txt"
/>




XML for second activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.manan.fibbo.Answer">

<TextView
android:id="@+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp" />




I'm probably doing a million things the wrong way here, this is just a mockup/test code. Please excuse my shitty programming practices.

Here is the thing I got from debug:

https://pastebin.com/19QtVLZD

Answer Source

You cannot have this initialization here:

EditText n = (EditText) findViewById(R.id.input_num);
Bundle bundle = new Bundle();

First create the variable as

private EditText n;
private Bundle bundle

then onCreate do this

 n = (EditText) findViewById(R.id.input_num);
 bundle = new Bundle();