在每个应用程序启动时,我的手机上都会显示一条错误消息:“应用程序已结束,抱歉”。我现在能做什么?我已经在网上搜索过了,我试图解决它,但我没有找到答案。
这是我的logcat输出:
FATAL EXCEPTION: main
Process: mycompany.kopfrechnenti1907_1839, PID: 524
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{mycompany.kopfrechnenti1907_1839/mycompany.kopfrechnenti1907_1839.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:2363)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$900(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5521)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:804)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:666)
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.AppCompatDelegate.create(AppCompatDelegate.java:203)
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 mycompany.kopfrechnenti1907_1839.MainActivity.<init>(MainActivity.java:10)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1069)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$900(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5521)
at java.lang.reflect.Method.invoke(Native Method)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RatingBar;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btnplus = (Button)findViewById(R.id.btnplus);
RatingBar ratingbar = (RatingBar)findViewById(R.id.ratingBar);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void plusonclick()
{
if (ratingbar.getRating() == 1)
{
setContentView(R.layout.activity_plusrechnenlvl1);
}
}
}
我的英语技能不是最好的,但我希望你能理解我:)
非常感谢!
月
答案 0 :(得分:0)
你必须在onCreate
之后写下
setContentView()
Button btnplus = (Button)findViewById(R.id.btnplus);
RatingBar ratingbar = (RatingBar)findViewById(R.id.ratingBar);
如下所示。
public class MainActivity extends AppCompatActivity {
Button btnplus;
RatingBar ratingbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnplus = (Button)findViewById(R.id.btnplus);
ratingbar = (RatingBar)findViewById(R.id.ratingBar);
}
public void plusonclick()
{
if (ratingbar.getRating() == 1)
{
setContentView(R.layout.activity_plusrechnenlvl1);
}
}
}