2017-06-14 158 views
-2

我對Android Studio非常陌生,我知道一點java.So我正在嘗試一個簡單的layout.I簡單的計算器應用程序。我希望我所有的代碼是正確的,但我不知道它有什麼問題。當我運行應用程序它並沒有打開,並顯示一個致命的異常main.Here是我的應用程序的XML代碼,Java代碼和logcat。所以請幫助我作出更正。謝謝!致命異常:main .... java.lang.RuntimeException:無法實例化活動ComponentInfo。?

XML代碼::

<?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代碼的::

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)  

回答

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(); 
    } 
} 

} 
0

只需修改的(圖INIT)位置, 這樣的代碼:

MainActivity{ 
    onCreate(){ 
    View view = findViewById(id); 
    } 
} 

注:活性是具有自生命週期的部件。所以不能將它作爲一個簡單的Pojo應用

相關問題