gpt4 book ai didi

java - 为什么这会导致 NullPointerException?

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

我有一个程序如下,谁能告诉我获得 NPE 的原因是什么。

public class MainActivity extends Activity {

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

secondClass x = new secondClass();
x.sText("Test Spring");

}

}



public class secondClass extends MainActivity {

public void sText(String s) {

TextView text = (TextView) findViewById(R.id.text);
text.setText(s);
}

}

主要音乐节

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/text"
/>

</RelativeLayout>

LogCat

08-16 01:31:11.320  15032-15032/? E/Trace: error opening trace file: No such file or directory (2)
08-16 01:31:11.420 836-987/? E/EmbeddedLogger: App crashed! Process: com.example.myapplication
08-16 01:31:11.420 836-987/? E/EmbeddedLogger: App crashed! Package: com.example.myapplication v1 (1.0)
08-16 01:31:11.420 836-987/? E/EmbeddedLogger: Application Label: My Application
08-16 01:31:11.420 15032-15032/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$600(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:110)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java)
at com.example.myapplication.secondClass.sText(secondClass.java:13)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)

据我了解,这应该实例化并初始化一个 secondaryClass 对象,然后调用它的 sText() 方法。然后,sText 方法应在 TextView 中存储引用。接下来,它调用 TextView 上的 setText() 方法,该方法将 TextView 中的文本更改为传递到 sText() 中的文本。

logcat 说它抛出 NullPointerException 但我不完全确定原因。如果这是一个菜鸟错误,那是因为我是一个菜鸟。谢谢。

最佳答案

您需要在第二个Activity 中重写onCreate 并设置setContentView(R.layout.mylayout)。然后你可以初始化你的textview。

您还需要使用 Intent 启动一个 Activity

     Intent intent = new Intent(MainActivtiy.this,SecondActivity.class);
startActivity(intent);

记住在 list 文件中为 Activity (SecondActivtiy)创建一个条目

如果您需要将字符串传递给第二个 Activity ,请使用intent.putExtra

      Intent intent = new Intent(MainActivtiy.this,SecondActivity.class);
intent.putExtra("key",mystring);
startActivity(intent);

然后在第二个Activity

    TextView tv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mysecondlayout);
tv = (TextView) findViewByid(R.id.textView1);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
tv.setText(value);
//get the value based on the key
}

http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String ,android.os.Bundle)

您可以findViewById设置到 Activity 的当前 View 层次结构。

此外,您还需要使用 Intent 启动Activity,而不是这样做econdClass x = new secondaryClass()

关于java - 为什么这会导致 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18266403/

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