我正在学习安卓编程。感谢您的帮助。
上次我在 android studio 中运行我的应用程序时,它运行完全且没有错误。第二天我运行该应用程序没有给出任何错误,但程序没有运行并在模拟器中显示:“不幸的是,我的 Application1 已停止”并且在 android studio 的运行部分中我遇到了这些情况:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication1, PID: 1701
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapplication1/com.example.myapplication1.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.myapplication1.MainActivity> cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.myapplication1.MainActivity> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
它的代码:
package com.example.myapplication1;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {
private static final String TAG= "MainActivity";
Button btn1, btn2;
ImageView imageView;
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.firstButton);
btn2=findViewById(R.id.secondButton);
imageView=findViewById(R.id.imageView);
btn1.setText(R.string.btn);
btn1.setTextColor(getResources().getColor(R.color.purple_700));
btn1.setBackgroundColor(getResources().getColor(R.color.teal_700));
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "click btn1");
}
});
btn2.setOnClickListener(this);
imageView.setScaleType(ImageView.ScaleType.FIT_START);
Toast.makeText(this,"salam",Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
Log.d(TAG, "click btn2");
}
}
问题出在哪里,有解决办法吗?