gpt4 book ai didi

java - android中的 fatal error 异常

转载 作者:行者123 更新时间:2023-11-30 02:36:00 25 4
gpt4 key购买 nike

我正在尝试编写 android 饮食计划器。调试后我发现当我的第一页淡出并且 Activity 想要转到第二页时发生 fatal error 异常但我找不到问题出在哪里?谁能帮帮我?

错误

10-24 06:35:46.622: E/AndroidRuntime(1102): FATAL EXCEPTION: main
10-24 06:35:46.622: E/AndroidRuntime(1102): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dietplanneraplication/com.example.dietplanneraplication.FeatureList}: java.lang.NullPointerException
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.os.Looper.loop(Looper.java:137)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-24 06:35:46.622: E/AndroidRuntime(1102): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 06:35:46.622: E/AndroidRuntime(1102): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 06:35:46.622: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-24 06:35:46.622: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 06:35:46.622: E/AndroidRuntime(1102): at dalvik.system.NativeStart.main(Native Method)
10-24 06:35:46.622: E/AndroidRuntime(1102): Caused by: java.lang.NullPointerException
10-24 06:35:46.622: E/AndroidRuntime(1102): at com.example.dietplanneraplication.FeatureList.onCreate(FeatureList.java:26)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.app.Activity.performCreate(Activity.java:5008)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-24 06:35:46.622: E/AndroidRuntime(1102): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-24 06:35:46.622: E/AndroidRuntime(1102): ... 11 more

XML 代码

MAINACTIVITY.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstpage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/logoid"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/logo_shadow4"
android:orientation="vertical" >

</LinearLayout>
</RelativeLayout>

特征列表.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/featurelist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.25"
android:background="#ffffff" >
</LinearLayout>
</LinearLayout>

BMICALCULATER>XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dietplanneraplication.BmiCalculaterPage"
tools:ignore="MergeRootFrame" />

Java 代码

MAINACTIVITY.JAVA

package com.example.dietplanneraplication;

public class MainActivity extends ActionBarActivity implements AnimationListener{
RelativeLayout screen;
int i;
Intent intent;
Animation animFadeout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen=(RelativeLayout) findViewById(R.id.firstpage);
animFadeout=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out );
animFadeout.setAnimationListener(MainActivity.this);

screen.post(new Runnable() {

@Override
public void run() {
screen.startAnimation(animFadeout);
animFadeout.setFillAfter(true);
}
});


}


@Override
public void onAnimationEnd(Animation anim) {
startActivity(new Intent(getApplicationContext(),FeatureList.class));
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();

}

@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub

}

}

FEATURELIST.JAVa

package com.example.dietplanneraplication;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class FeatureList extends ActionBarActivity {
Button bmiinformation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feature_list);
bmiinformation = (Button) findViewById(R.id.bmiinformation);
bmiinformation.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent =new Intent(getApplicationContext(),BmiCalculaterPage.class);
startActivity(intent);
finish();

}
});
}

BMICALCULATER.JAVA

public class BmiCalculaterPage extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi_calculater_page);


}

最佳答案

您的 activity_feature_list layout.xml 中没有名为 bmiinformation 的 ID。

 bmiinformation = (Button) findViewById(R.id.bmiinformation); // bmiinformation is null as a result

这就是我认为的问题所在。
尝试添加一个 ID 为 bmiinformation 的按钮。

关于java - android中的 fatal error 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26542967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com