//I'm new to programming and android studio
package com.example.calc;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static com.example.calc.R.id.editText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText editTexts = (EditText) findViewById(editText);
double a = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Buttons to take input and display
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
//using to make the buttons react
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
}
@Override
public void onClick (View v){
double res; //Display and taking input from same textfield
if (TextUtils.isEmpty(editTexts.getText().toString())) {
return;
}
res = Double.parseDouble(editTexts.getText().toString());
switch (v.getId()) {
case R.id.button1:
a += res;
editTexts.setText(" ");
break;
case R.id.button2:
a -= res;
editTexts.setText(" ");
break;
case R.id.button3:
a *= res;
editTexts.setText(" ");
break;
case R.id.button4:
a /= res;
editTexts.setText(" ");
break;
case R.id.button5:
editTexts.setText((int) a);
break;
case R.id.button6:
a = 0.0;
editTexts.setText(" ");
break;
}
}
}
不幸的是,App崩溃了。 Gradle正在建设,但一打开就崩溃了。 如何解决这个错误??
logCat:
06-24 22:36:42.926 4983-4983/? E/libprocessgroup: failed to make and chown
/acct/uid_10058: Read-only file system
06-24 22:36:42.926 4983-4983/? W/Zygote: createProcessGroup failed, kernel
missing CONFIG_CGROUP_CPUACCT?
06-24 22:36:42.930 4983-4983/? I/art: Not late-enabling -Xcheck:jni (already
on)
06-24 22:36:43.400 4983-4983/com.example.calc I/InstantRun: starting instant
run server: is main process
06-24 22:36:43.626 4983-4983/com.example.calc W/art: Verification of boolean
android.support.v7.app.AppCompatDelegateImplV9.initializePanelContent(android.support.v7.app.AppCompatDelegateImplV9$PanelFeatureState) took 139.729ms
06-24 22:36:43.741 4983-4983/com.example.calc W/art: Verification of
android.support.v7.view.ActionMode
android.support.v7.app.AppCompatDelegateImplV9.startSupportActionModeFromWindow(android.support.v7.view.ActionMode$Callback) took 109.701ms
06-24 22:36:43.743 4983-4983/com.example.calc D/AndroidRuntime: Shutting down VM
--------- beginning of crash
06-24 22:36:43.746 4983-4983/com.example.calc E/AndroidRuntime: FATAL
EXCEPTION: main
Process:
com.example.calc, PID: 4983
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.calc/com.example.calc.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:2236)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at
android.app.ActivityThread.access$800(ActivityThread.java:151)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at
android.os.Handler.dispatchMessage(Handler.java:102)
at
android.os.Looper.loop(Looper.java:135)
at
android.app.ActivityThread.main(ActivityThread.java:5254)
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)
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:118)
at
android.support.v7.app.AppCompatDelegateImplV9.<init>
(AppCompatDelegateImplV9.java:152)
at
android.support.v7.app.AppCompatDelegateImplV11.<init>
(AppCompatDelegateImplV11.java:29)
at
android.support.v7.app.AppCompatDelegateImplV14.<init>
(AppCompatDelegateImplV14.java:53)
at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:204)
at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:184)
at
android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:518
)
at
android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:189
)
at
com.example.calc.MainActivity.<init>(MainActivity.java:15)
at
java.lang.reflect.Constructor.newInstance(Native Method)
at
java.lang.Class.newInstance(Class.java:1606)
at
android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at
android.app.ActivityThread.access$800(ActivityThread.java:151)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at
android.os.Handler.dispatchMessage(Handler.java:102)
at
android.os.Looper.loop(Looper.java:135)
at
android.app.ActivityThread.main(ActivityThread.java:5254)
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)