I am new to Android and I started making my first application following tutorials and such. However, when I click the run button it gives me following error on the logcat from which I count not identify where the error is. Hence, here is my code in hope of some advice. Thanks in advance.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText firstnum = (EditText) findViewById(R.id.numberinput);
TextView resultTextView = (TextView) findViewById(R.id.resulttextview);
int num1 = Integer.parseInt(firstnum.getText().toString()) ;
int result = num1 * num1;
resultTextView.setText(result + "");
}
});
}
}
LogCat after fixing the render problem :
7-16 01:27:14.359 10870-10870/com.example.abarimess.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abarimess.myfirstapp, PID: 10870
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abarimess.myfirstapp/com.example.abarimess.myfirstapp.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x4
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294)
at android.app.ActivityThread.access$1000(ActivityThread.java:210)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelOffset(TypedArray.java:546)
at android.support.constraint.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2685)
at android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1915)
at android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:476)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:820)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:467)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.abarimess.myfirstapp.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6575)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294)
at android.app.ActivityThread.access$1000(ActivityThread.java:210)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
And the layout 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=".MainActivity"
tools:layout_editor_absoluteY="73dp">
<TextView
android:id="@+id/textView3"
android:layout_width="360dp"
android:layout_height="119dp"
android:layout_marginTop="50dp"
android:text="@string/square_of_the_number_is"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@android:color/holo_purple"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/numberinput" />
<TextView
android:id="@+id/Resulttextview"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="@android:color/holo_red_dark"
android:text="@string/the_result_is"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="@android:color/darker_gray"
android:text="@string/_0"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Resulttextview" />
<Button
android:id="@+id/btn"
android:layout_width="370dp"
android:layout_height="80dp"
android:layout_marginTop="50dp"
android:background="?android:attr/colorActivatedHighlight"
android:text="@string/butan"
android:textColor="@android:color/holo_blue_bright"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<EditText
android:id="@+id/numberinput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="@string/number_in_here"
android:inputType="numberDecimal"
android:singleLine="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resulttextview" />
</android.support.constraint.ConstraintLayout>
the manifest.Xml as requested in the comments :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abarimess.myfirstapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In your layout xml file, you have two separate TextView
which are the following.
<TextView
android:id="@+id/Resulttextview"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="@android:color/holo_red_dark"
android:text="@string/the_result_is"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="@android:color/darker_gray"
android:text="@string/_0"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Resulttextview" />
Name of both text views are identical except the first one has started with a capital R
. You might consider the name of the first TextView
to something else like resulttextview1
like the following.
<TextView
android:id="@+id/resulttextview1"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="@android:color/holo_red_dark"
android:text="@string/the_result_is"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="@android:color/darker_gray"
android:text="@string/_0"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resulttextview1" />
Please avoid naming layout identifiers with capital letters. Use underscores instead (e.g. result_text_view
).
Another potential problem in your code is, if there is nothing entered in your EditText
the app will crash again with a NullPointerException
as the value which will be returned from firstnum.getText().toString()
will have the null value.
In this case, you might consider adding a null checking here.
@Override
public void onClick(View v) {
EditText firstnum = (EditText) findViewById(R.id.numberinput);
TextView resultTextView = (TextView) findViewById(R.id.resulttextview);
// Add a null check here for safety
if(firstnum.getText().toString() == null) return;
int num1 = Integer.parseInt(firstnum.getText().toString()) ;
int result = num1 * num1;
resultTextView.setText(result + "");
}
You will get NumberFormatException
in case of entering a string in your EditText
other than a number. So you might need to handle that case as well. However, this can be ignored in case of handling only numbers in your EditText
.
this Is because you should to instance
EditText firstnum = (EditText) findViewById(R.id.numberinput);
Out of method on click listener.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With