gpt4 book ai didi

android - setText错误,从2个 Activity 传递值时,Android应用程序强制退出

转载 作者:行者123 更新时间:2023-12-03 16:53:10 25 4
gpt4 key购买 nike

因此,我尝试将第一个 Activity 中计算出的结果传递给第二个 Activity 中必须显示的结果。我正在使用 putExtra 。问题是当我尝试在 TextView “wyn”中设置文本时。我还尝试在第二个 Activity 中将 seText(“text”)制作成它也不起作用。
更改 Activity 时程序崩溃:/
感谢帮助 :)

改变 Activity 的方法

public void calculateClick(View view){
if(view.getId()==R.id.oblicz){
EditText wag = (EditText)findViewById(R.id.waga);
EditText wzr = (EditText)findViewById(R.id.wzrost);
RadioGroup group = (RadioGroup)findViewById(R.id.group);
TextView res = (TextView)findViewById(R.id.result);
if(wag ==null || wzr ==null || group == null){
Toast.makeText(Page1Activity.this, "Podaj dane !", Toast.LENGTH_SHORT).show();
Intent intent = getIntent();
finish();
startActivity(intent);
}
float fwaga = Float.parseFloat(wag.getText().toString());
float fwzrost = Float.parseFloat(wzr.getText().toString());
if( fwzrost <= 0){
Toast.makeText(Page1Activity.this, "Coś za mały jesteś nie ?", Toast.LENGTH_SHORT).show();
Intent intent = getIntent();
finish();
startActivity(intent);
}
if(group.getCheckedRadioButtonId() == R.id.rb1){
fwzrost = fwzrost /100;
}

float rez = (float) (fwaga / (fwzrost * fwzrost));
Intent intent = new Intent(Page1Activity.this, Page1resActivity.class);
intent.putExtra(EXTRA_MESSAGE, "BMI = " + rez);
final int result = 1;
startActivityForResult(intent, result);
}
}

我必须展示结果的第二项 Activity
public class Page1resActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1res);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

Intent intent = getIntent();
String message = intent.getStringExtra(Page1Activity.EXTRA_MESSAGE);
TextView res = ((TextView) findViewById(R.id.wyn));
res.setText(message); //ERROR HERE ?

}

secondActivity的XML
<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: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.labswm.Page1resActivity$PlaceholderFragment"
tools:ignore="HardcodedText,UselessParent" >

<LinearLayout
android:id="@+id/lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical" >

<TextView
android:id="@+id/opispagewyniki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/opis_to_okno_pozwala_na_sprawdzenie_swojego_bmi_i_por_wnanie_go_" />
//THIS IS THE TEXTVIEW I USE TO PUT RESULT
<TextView
android:id="@+id/wyn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//******************************************************//
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dla_os_b_doros_ych_wartosc_bmi_wskazuje_na_" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="&lt; 15,0 - wyniszczenie"
android:textSize="15sp" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_15_0_17_4_wychudzenie"
android:textSize="15sp" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_17_5_19_4_niedowaga"
android:textSize="15sp" />

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_19_5_24_9_waga_prawid_owa"
android:textSize="15sp" />

<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_25_0_29_9_nadwaga"
android:textSize="15sp" />

<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_30_0_34_9_i_stopie_oty_o_ci"
android:textSize="15sp" />

<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_35_0_39_9_ii_stopie_oty_o_ci"
android:textSize="15sp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_40_0_oty_o_kliniczna"
android:textSize="15sp" />

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dla leniwych !" />

</LinearLayout>

日志
04-17 17:50:17.760: D/dalvikvm(16620): GC_CONCURRENT freed 132K, 46% free 3078K/5639K,  external 251K/1281K, paused 3ms+3ms
04-17 17:50:24.360: W/dalvikvm(16620): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-17 17:50:24.370: E/AndroidRuntime(16620): FATAL EXCEPTION: main
04-17 17:50:24.370: E/AndroidRuntime(16620): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.labswm/com.example.labswm.Page1resActivity}: java.lang.NullPointerException
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.os.Looper.loop(Looper.java:130)
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.app.ActivityThread.main(ActivityThread.java:3691)
04-17 17:50:24.370: E/AndroidRuntime(16620): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 17:50:24.370: E/AndroidRuntime(16620): at java.lang.reflect.Method.invoke(Method.java:507)
04-17 17:50:24.370: E/AndroidRuntime(16620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-17 17:50:24.370: E/AndroidRuntime(16620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-17 17:50:24.370: E/AndroidRuntime(16620): at dalvik.system.NativeStart.main(Native Method)
04-17 17:50:24.370: E/AndroidRuntime(16620): Caused by: java.lang.NullPointerException
04-17 17:50:24.370: E/AndroidRuntime(16620): at com.example.labswm.Page1resActivity.onCreate(Page1resActivity.java:30)
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
04-17 17:50:24.370: E/AndroidRuntime(16620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-17 17:50:24.370: E/AndroidRuntime(16620): ... 11 more
04-17 17:50:26.380: I/dalvikvm(16620): threadid=4: reacting to signal 3
04-17 17:50:26.380: I/dalvikvm(16620): Wrote stack traces to '/data/anr/traces.txt'
04-17 17:50:40.120: I/dalvikvm(16620): threadid=4: reacting to signal 3
04-17 17:50:40.130: I/dalvikvm(16620): Wrote stack traces to '/data/anr/traces.txt'

最佳答案

您正在将以下布局设置为contentView:

setContentView(R.layout.activity_page1res);

但您正在搜索此布局中不包含的ID(因为textview位于fragment_page1res.xml布局中):
TextView res =  ((TextView) findViewById(R.id.wyn));
res.setText(message); //ERROR HERE ?

==>这就是为什么 findViewById尝试在res上调用setText后返回null ==> res == null ==> NPE的原因。

关于android - setText错误,从2个 Activity 传递值时,Android应用程序强制退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23137874/

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