我是Android Studio的新手,我知道一点java.So我正在尝试一个简单布局的简单计算器应用程序。我希望我的所有代码都是正确的,但我不知道它有什么问题。当时我运行应用程序它根本没有打开并显示一个FATAL异常main.Here是我的app的xml代码,java代码和logcat。所以请帮我修正。谢谢!
XML Code ::
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns: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:background="@color/colorPrimary"
tools:context="com.example.narendra.calculator.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="@+id/editText"
android:layout_width="360dp"
android:layout_height="159dp"
android:background="@android:color/holo_blue_light"
android:ems="10"
android:hint="@string/num"
android:inputType="number"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="50sp"
android:textStyle="bold"
tools:ignore="MissingConstraints,RtlHardcoded"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="360dp"
android:layout_height="159dp"
android:background="@android:color/holo_green_light"
android:ems="10"
android:hint="@string/Num2"
android:inputType="number"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="50sp"
android:textStyle="bold"
tools:ignore="MissingConstraints,RtlHardcoded"
tools:layout_editor_absoluteY="180dp"
tools:layout_editor_absoluteX="26dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onAddButtonClick"
android:text="@string/a"
android:textSize="50sp"
app:layout_constraintLeft_toLeftOf="@+id/editText2"
app:layout_constraintTop_toTopOf="@+id/editText"
android:layout_marginTop="323dp"
tools:ignore="RtlHardcoded"
app:layout_constraintRight_toLeftOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSubtractionButtonClick"
android:text="@string/m"
android:textSize="50sp"
android:layout_marginLeft="5dp"
app:layout_constraintTop_toTopOf="@+id/editText"
android:layout_marginTop="323dp"
tools:ignore="RtlHardcoded"
app:layout_constraintLeft_toRightOf="@+id/button"
app:layout_constraintHorizontal_bias="0.03"
android:layout_marginRight="5dp"
app:layout_constraintRight_toRightOf="@+id/button3"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onMultiplicationButtonClick"
android:text="@string/p"
android:textSize="50sp"
android:layout_marginLeft="5dp"
app:layout_constraintTop_toTopOf="@+id/editText"
android:layout_marginTop="323dp"
tools:ignore="RtlHardcoded"
app:layout_constraintLeft_toRightOf="@+id/button2"
android:layout_marginRight="5dp"
app:layout_constraintRight_toRightOf="@+id/button4"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDivisionButtonClick"
android:text="@string/d"
android:textSize="50sp"
android:layout_marginLeft="5dp"
app:layout_constraintRight_toRightOf="@+id/editText2"
app:layout_constraintTop_toTopOf="@+id/editText"
android:layout_marginTop="323dp"
tools:ignore="RtlHardcoded"
app:layout_constraintLeft_toRightOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/textView"
android:layout_width="360dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:background="@color/colorAccent"
android:text="@string/Result"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="50sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.771"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.839"
tools:ignore="RtlHardcoded" />
Java Code ::
package com.example.narendra.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText e1=(EditText)findViewById(R.id.editText);
EditText e2=(EditText)findViewById(R.id.editText2);
TextView t1=(TextView)findViewById(R.id.textView);
int num1=Integer.parseInt(e1.getText().toString());
int num2=Integer.parseInt(e2.getText().toString());
public void conditionCheck(){
if(e1.getText().toString().isEmpty()){
e1.setError("Please Enter a valid number");
}
else if(e2.getText().toString().isEmpty()){
e2.setError("Please Enter a valid number");
}
}
public void onAddButtonClick(View v){
conditionCheck();
int sum=num1+num2;
t1.setText(sum);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onSubtractionButtonClick(View v){
conditionCheck();
int diff=num1-num2;
t1.setText(diff);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onMultiplicationButtonClick(View v){
conditionCheck();
int product=num1*num2;
t1.setText(product);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onDivisionButtonClick(View v){
conditionCheck();
if(num2==0){
e2.setError(" Zero is not allowed here");
}
else {
int quotient = num1 / num2;
t1.setText(quotient);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
}
}
Logcat ::
06-14 12:31:23.216 4822-4822/? I/art: Not late-enabling -Xcheck:jni (already on)
06-14 12:31:23.216 4822-4822/? W/art: Unexpected CPU variant for X86 using defaults: x86
06-14 12:31:23.342 4822-4822/? W/System: ClassLoader referenced unknown path: /data/app/com.example.narendra.calculator-2/lib/x86
06-14 12:31:23.348 4822-4822/? I/InstantRun: starting instant run server: is main process
06-14 12:31:23.405 4822-4822/? D/AndroidRuntime: Shutting down VM
06-14 12:31:23.405 4822-4822/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.narendra.calculator, PID: 4822
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.narendra.calculator/com.example.narendra.calculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:155)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.narendra.calculator.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
答案 0 :(得分:1)
你的onCreate()提早关闭
将其更改为
package com.example.narendra.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
int num1,num2;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText);
e2 = (EditText) findViewById(R.id.editText2);
t1 = (TextView) findViewById(R.id.textView);
num1 = Integer.parseInt(e1.getText().toString());
num2 = Integer.parseInt(e2.getText().toString());
} public void conditionCheck(){
if(e1.getText().toString().isEmpty()){
e1.setError("Please Enter a valid number");
}
else if(e2.getText().toString().isEmpty()){
e2.setError("Please Enter a valid number");
}
}
public void onAddButtonClick(View v){
conditionCheck();
int sum=num1+num2;
t1.setText(sum);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onSubtractionButtonClick(View v){
conditionCheck();
int diff=num1-num2;
t1.setText(diff);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onMultiplicationButtonClick(View v){
conditionCheck();
int product=num1*num2;
t1.setText(product);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onDivisionButtonClick(View v){
conditionCheck();
if(num2==0){
e2.setError(" Zero is not allowed here");
}
else {
int quotient = num1 / num2;
t1.setText(quotient);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:0)
只需修改(view init)位置的代码, 像这样:
MainActivity{
onCreate(){
View view = findViewById(id);
}
}
注意:Activity是具有自我生命周期的组件。所以不能将其应用为简单的Pojo