gpt4 book ai didi

android - 传入文本值并尝试显示时 Activity 崩溃

转载 作者:行者123 更新时间:2023-11-29 17:51:07 26 4
gpt4 key购买 nike

获取用户输入:

Intent intentTrashcanLocation = new Intent(this, TrashcanLocationActivity.class);           
EditText editWidth = (EditText)findViewById(R.id.dimvalWidth);
EditText editLength = (EditText)findViewById(R.id.dimvalLength);
String width = editWidth.getText().toString();
String length = editLength.getText().toString();
intentTrashcanLocation.putExtra("widthValue", width);
intentTrashcanLocation.putExtra("lengthValue", length);
startActivity(intentTrashcanLocation);

接收和显示:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trashcan_location);
Intent parentintent = getIntent();
Bundle dimval = parentintent.getExtras();
String width = (String)dimval.get("widthValue");
String length = (String)dimval.get("lengthValue");
TextView widthtext = (TextView)findViewById(R.id.widthView);
TextView lengthtext = (TextView)findViewById(R.id.lengthView);
widthtext.setText(width);
lengthtext.setText(length);

当我运行程序并输入文本并继续进行下一个 Activity 时,程序崩溃。获取错误:

03-29 12:33:57.418: E/AndroidRuntime(28049): FATAL EXCEPTION: main
03-29 12:33:57.418: E/AndroidRuntime(28049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trash2/com.example.trash2.TrashcanLocationActivity}: java.lang.NullPointerException
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.os.Looper.loop(Looper.java:137)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-29 12:33:57.418: E/AndroidRuntime(28049): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 12:33:57.418: E/AndroidRuntime(28049): at java.lang.reflect.Method.invoke(Method.java:525)
03-29 12:33:57.418: E/AndroidRuntime(28049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-29 12:33:57.418: E/AndroidRuntime(28049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-29 12:33:57.418: E/AndroidRuntime(28049): at dalvik.system.NativeStart.main(Native Method)
03-29 12:33:57.418: E/AndroidRuntime(28049): Caused by: java.lang.NullPointerException
03-29 12:33:57.418: E/AndroidRuntime(28049): at com.example.trash2.TrashcanLocationActivity.onCreate(TrashcanLocationActivity.java:36)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.app.Activity.performCreate(Activity.java:5133)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 12:33:57.418: E/AndroidRuntime(28049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-29 12:33:57.418: E/AndroidRuntime(28049): ... 11 more

我对此很陌生,非常感谢任何帮助。谢谢!

编辑:这是 activity_trashcan_location:

<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.trash2.TrashcanLocationActivity"
tools:ignore="MergeRootFrame" />

和 fragment_trashcan_location:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/interface_bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.trash2.TrashcanLocationActivity$PlaceholderFragment" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:text="Bluetooth Trashcan"
android:textAppearance="@style/AppTheme"
android:textSize="60dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="10dp"
android:text="- Settings: Trashcan Location?"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40dp" />

<TextView
android:id="@+id/widthView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/lengthView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/buttonNext"
android:layout_width="140dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="19dp"
android:layout_marginRight="20dp"
android:text="Finish"
android:textSize="30dp"
android:onClick="doMain" />

最佳答案

您的 activity_trashcan_location 布局不包含 View widthViewlengthView。相反,它们位于 fragment 布局中,并且它不会成为 Activity onCreate() 中 Activity View 层次结构的一部分。

要么修复它

  • 将接触 View 的代码移动到 fragment 中,例如它的onCreateView(),或者

  • 将需要触摸的 View 移动到 Activity 布局中。

关于android - 传入文本值并尝试显示时 Activity 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734517/

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