这是问题所在
E / Android运行时:致命异常:主要 流程:abuhashesh.homam.myfirstapp,PID:5379 java.lang.RuntimeException:无法实例化活动 ComponentInfo {abuhashesh.homam.myfirstapp / abuhashesh.homam.myfirstapp.Activity_2}: java.lang.NullPointerException:尝试调用虚拟方法 上的“ android.view.Window $ Callback android.view.Window.getCallback()” 空对象引用 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 在android.app.ActivityThread.access $ 800(ActivityThread.java:144) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5221) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 原因:java.lang.NullPointerException:尝试调用虚拟方法'android.view.Window $ Callback 空对象引用上的android.view.Window.getCallback()' 在android.support.v7.app.AppCompatDelegateImplBase。(AppCompatDelegateImplBase.java:117) 在android.support.v7.app.AppCompatDelegateImplV9。(AppCompatDelegateImplV9.java:149) 在android.support.v7.app.AppCompatDelegateImplV14。(AppCompatDelegateImplV14.java:56) 在android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202) 在android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183) 在android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519) 在android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190) 在abuhashesh.homam.myfirstapp.Activity_2。(Activity_2.java:144) 在java.lang.reflect.Constructor.newInstance(本地方法) 在java.lang.Class.newInstance(Class.java:1572) 在android.app.Instrumentation.newActivity(Instrumentation.java:1065) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 在android.app.ActivityThread.access $ 800(ActivityThread.java:144) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5221) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我的第一个活动课:
package abuhashesh.homam.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Calendar;
public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Button gototwo=(Button)findViewById(R.id.button2);
gototwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent ionetotwo=new
Intent(getApplicationContext(),Activity_2.class);
startActivity(ionetotwo);
}
});
}
public void Buage(View view) {
EditText Year=(EditText)findViewById(R.id.editText);
Calendar c=Calendar.getInstance();
int Current_year =c.get(Calendar.YEAR);
int YearOfBirth=Integer.parseInt(Year.getText().toString());
int age=Current_year-YearOfBirth;
Toast.makeText(this,"age:"+String.valueOf(age),Toast.LENGTH_LONG).show();
}
}
my second activity class:
package abuhashesh.homam.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
公共类Activity_2扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"0");
}
});
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"3");
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"9");
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"0");
}
});
sum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"0");
}
});
min.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"0");
}
});
multi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"0");
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText(theoper.getText().toString()+"0");
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theoper.setText("");
}
});
}
Button zero=(Button)findViewById(R.id.bu10);
Button one=(Button)findViewById(R.id.bu1);
Button two=(Button)findViewById(R.id.bu2);
Button three=(Button)findViewById(R.id.bu3);
Button four=(Button)findViewById(R.id.bu4);
Button five=(Button)findViewById(R.id.bu5);
Button six=(Button)findViewById(R.id.bu6);
Button seven=(Button)findViewById(R.id.bu7);
Button eight=(Button)findViewById(R.id.bu8);
Button nine=(Button)findViewById(R.id.bu9);
Button clear=(Button)findViewById(R.id.buclear);
Button sum=(Button)findViewById(R.id.bu11);
Button min=(Button)findViewById(R.id.bu12);
Button multi=(Button)findViewById(R.id.bu13);
Button div=(Button)findViewById(R.id.bu14);
Button equal=(Button)findViewById(R.id.bu15);
TextView theoper=(TextView)findViewById(R.id.textView4);
TextView Res=(TextView)findViewById(R.id.textView5);
}