gpt4 book ai didi

java - AndroidAnnotations 运行时错误

转载 作者:行者123 更新时间:2023-12-01 18:39:12 26 4
gpt4 key购买 nike

我在我的应用程序中使用 AndroidAnnotations,当我尝试将额外内容放入 Intent 并启动一个使用 AndroidAnnotations 的 Activity 时,我总是会收到运行时错误。当我停止使用 AndroidAnnotations 时,一切正常。

这是一个代码,它启动我的 Activity :

public class TimeAndDateShower extends Activity {
//some code

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time_and_date_shower);
//some code
setButtonListener();

}
public void setButtonListener()
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(TimeAndDateShower.this, DateChooser_.class);
intent.putExtra("SSID", network);
startActivity(intent);
TimeAndDateShower.this.finish();
}
});
}
}

这是 DateChooser.java 的样子:

@EActivity(R.layout.date_chooser)
public class DateChooser extends Activity {

public String network;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.date_chooser);

setNetworkName();
//some code
}

public void setNetworkName()
{
TextView textView = (TextView)findViewById(R.id.textView4);
network = this.getIntent().getStringExtra("SSID");//using an extra
textView.setText(network);
}

}

在 AndroidManifest.xml 中,我声明 DateChooser Activity ,如下所示:

name="com.componentix.imwizard.DateChooser_" android:screenOrientation="landscape"> </activity>

这是我的运行时错误日志:

12-17 11:51:32.267: E/AndroidRuntime(3234): FATAL EXCEPTION: main
12-17 11:51:32.267: E/AndroidRuntime(3234): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.componentix.imwizard/com.componentix.imwizard.DateChooser_}: java.lang.NullPointerException
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.os.Looper.loop(Looper.java:137)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-17 11:51:32.267: E/AndroidRuntime(3234): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 11:51:32.267: E/AndroidRuntime(3234): at java.lang.reflect.Method.invoke(Method.java:511)
12-17 11:51:32.267: E/AndroidRuntime(3234): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-17 11:51:32.267: E/AndroidRuntime(3234): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-17 11:51:32.267: E/AndroidRuntime(3234): at dalvik.system.NativeStart.main(Native Method)
12-17 11:51:32.267: E/AndroidRuntime(3234): Caused by: java.lang.NullPointerException
12-17 11:51:32.267: E/AndroidRuntime(3234): at com.componentix.imwizard.DateChooser.setNetworkName(DateChooser.java:30)
12-17 11:51:32.267: E/AndroidRuntime(3234): at com.componentix.imwizard.DateChooser.onCreate(DateChooser.java:22)
12-17 11:51:32.267: E/AndroidRuntime(3234): at com.componentix.imwizard.DateChooser_.onCreate(DateChooser_.java:24)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.app.Activity.performCreate(Activity.java:5008)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-17 11:51:32.267: E/AndroidRuntime(3234): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-17 11:51:32.267: E/AndroidRuntime(3234): ... 11 more

最佳答案

我猜你的 NPE 位于 textView 上,这很正常:布局注入(inject)是在原始 oncreate() 方法之后在生成的类中完成的。另外,你应该让AA为你注入(inject)额外的和观点。只需像这样更新您的类(class)即可:

@EActivity(R.layout.date_chooser)
public class DateChooser extends Activity {

@Extra("SSID")
String network;

@ViewById(R.id.textView4)
TextView textView;

@AfterViews
void init() {
textView.setText(network);
//some code
}

}

你真的应该仔细看看at the wiki

关于java - AndroidAnnotations 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20630927/

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