Android Sudio - 基本计算器 - 应用程序显示不幸被停止

时间:2016-02-07 00:59:40

标签: android

我是使用android studio的新手。我尝试了这段代码并构建并生成了一个未签名的apk。我试过三星Galaxy潮流GT-s7392和华硕Zenfone 5,也尝试过蓝图。但是当应用程序打开时,它会显示 - “不幸的是计算器停止了。?”

此外,它永远不会要求在安装时获得任何许可。我是否需要修改android manifest.xml或任何其他文件

package in.harisree.calculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public abstract class MainActivity extends AppCompatActivity implements OnClickListener {

    public Button btnAdd,btnSub,btnMul,btnDiv;
    public TextView tvResult;
    public EditText etNum1,etNum2;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    private void init() {
        btnAdd=(Button)findViewById(R.id.btnAdd);
        btnSub=(Button)findViewById(R.id.btnSub);
        btnMul=(Button)findViewById(R.id.btnMul);
        btnDiv=(Button)findViewById(R.id.btnDiv);
        etNum1=(EditText)findViewById(R.id.etNum1);
        etNum2=(EditText)findViewById(R.id.etNum2);
        tvResult=(TextView)findViewById(R.id.tvResult);

        btnAdd.setOnClickListener(this);
        btnSub.setOnClickListener(this);
        btnMul.setOnClickListener(this);
        btnDiv.setOnClickListener(this);
    }

    public void onClick(View view)
    {
        String a= String.valueOf(etNum1.getText());
        String b= String.valueOf(etNum1.getText());

        switch (view.getId())
        {
            case R.id.btnAdd:
                tvResult.setText(String.valueOf(Integer.parseInt(a)+Integer.parseInt(b)));
                break;
            case R.id.btnSub:
                tvResult.setText(String.valueOf(Integer.parseInt(a)-Integer.parseInt(b)));
                break;
            case R.id.btnMul:
                tvResult.setText(String.valueOf(Integer.parseInt(a)*Integer.parseInt(b)));
                break;
            case R.id.btnDiv:
                tvResult.setText(String.valueOf(Integer.parseInt(a)/Integer.parseInt(b)));
                break;
        }
    }
}

这里是logcat

02-14 18:34:44.287 15923-15923/? I/art: Late-enabling -Xcheck:jni
02-14 18:34:44.437 15923-15923/in.harisree.calculator V/Monotype: SetAppTypeFace- try to flip, app = in.harisree.calculator
02-14 18:34:44.437 15923-15923/in.harisree.calculator V/Monotype:     Typeface getFontPathFlipFont - systemFont = default
02-14 18:34:44.457 15923-15923/in.harisree.calculator D/AndroidRuntime: Shutting down VM
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime: FATAL EXCEPTION: main
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime: Process: in.harisree.calculator, PID: 15923
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{in.harisree.calculator/in.harisree.calculator.MainActivity}: java.lang.InstantiationException: class in.harisree.calculator.MainActivity cannot be instantiated
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:147)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:  Caused by: java.lang.InstantiationException: class in.harisree.calculator.MainActivity cannot be instantiated
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at java.lang.Class.newInstance(Class.java:1553)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2217)
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:147) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
02-14 18:34:44.457 15923-15923/in.harisree.calculator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

2 个答案:

答案 0 :(得分:0)

实现OnClickListener时,请确保覆盖onClickListerner。

package in.harisree.calculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public abstract class MainActivity extends AppCompatActivity implements OnClickListener {

public Button btnAdd,btnSub,btnMul,btnDiv;
public TextView tvResult;
public EditText etNum1,etNum2;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();

}

private void init() {
    btnAdd=(Button)findViewById(R.id.btnAdd);
    btnSub=(Button)findViewById(R.id.btnSub);
    btnMul=(Button)findViewById(R.id.btnMul);
    btnDiv=(Button)findViewById(R.id.btnDiv);
    etNum1=(EditText)findViewById(R.id.etNum1);
    etNum2=(EditText)findViewById(R.id.etNum2);
    tvResult=(TextView)findViewById(R.id.tvResult);

    btnAdd.setOnClickListener(this);
    btnSub.setOnClickListener(this);
    btnMul.setOnClickListener(this);
    btnDiv.setOnClickListener(this);
}

@override
public void onClick(View v)
{
    String a= String.valueOf(etNum1.getText());
    String b= String.valueOf(etNum1.getText());

    switch (view.getId())
    {
        case R.id.btnAdd:
            tvResult.setText(String.valueOf(Integer.parseInt(a)+Integer.parseInt(b)));
            break;
        case R.id.btnSub:
            tvResult.setText(String.valueOf(Integer.parseInt(a)-Integer.parseInt(b)));
            break;
        case R.id.btnMul:
            tvResult.setText(String.valueOf(Integer.parseInt(a)*Integer.parseInt(b)));
            break;
        case R.id.btnDiv:
            tvResult.setText(String.valueOf(Integer.parseInt(a)/Integer.parseInt(b)));
            break;
    }
}
}

答案 1 :(得分:0)

您应该删除摘要(如果这是您在清单中定义的活动)。因为android.app.Instrumentation在运行应用程序时无法实例化该活动。您可以创建基本抽象活动,MainAcitvity可以扩展它。

您的代码应抛出此异常 java.lang.InstantiationException:无法实例化类in.harisree.calculator.MainActivity