第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將信息從一個 java 類傳遞到另一個

如何將信息從一個 java 類傳遞到另一個

慕慕森 2023-03-17 15:30:23
我需要將一個參數(shù)從一個 Java 類 (A) 傳遞到另一個 Java 類 (B)。我使用了互聯(lián)網(wǎng)上的許多解決方案,但都無法解決我的問題。用戶將從單選按鈕列表中選擇他們的答案。分數(shù)會加起來,我需要把分數(shù)傳給B班。在B類中,用戶繼續(xù)回答問題,我需要將A類和B類的分數(shù)相加得到最終分數(shù),并將其顯示在B類的底部。當(dāng)我點擊 A 類中的按鈕時,應(yīng)用程序保持停止。但我認為問題出在B類。有誰知道如何解決這個問題?太感謝了。一類 private int score;protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.a);        Button btn = findViewById(R.id.anxnext);        final RadioGroup rg1 =  findViewById(R.id.anxq1g);        final RadioGroup rg2 =  findViewById(R.id.anxq2g);btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // Get the checked Radio Button ID from Radio Group                int g1 = rg1.getCheckedRadioButtonId();                int g2 = rg2.getCheckedRadioButtonId();                if (g1 != -1) {                    View radioButton = rg1.findViewById(g1);                    idx1 = rg1.indexOfChild(radioButton);                }                if (g2 != -1) {                    View radioButton = rg2.findViewById(g2);                    idx2 = rg2.indexOfChild(radioButton);                }              score=idx1+idx2;        Intent intent = new Intent(A.this, B.class);                intent.putExtra("message", score);                startActivity(intent);            }        });    }B類 private int score1,totalscore; protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.b);        Bundle extras = getIntent().getExtras();        if(extras!=null) {            String m= extras.getString("message");            totalscore=Integer.parseInt(m);        }下面顯示在logcat中
查看完整描述

2 回答

?
慕后森

TA貢獻1802條經(jīng)驗 獲得超5個贊

注意數(shù)據(jù)類型:

在A班,你有:

score=idx1+idx2; 
       Intent intent = new Intent(A.this, B.class);
                intent.putExtra("message", score);
                startActivity(intent);

score是 int,但是在 B 類中:

Bundle extras = getIntent().getExtras(); 
       if(extras!=null) {
            String m= extras.getString("message");
            totalscore=Integer.parseInt(m);
        }

您正試圖將其作為字符串獲取,但它為空,因此應(yīng)用程序崩潰了。

所以請將其更改為:

Bundle extras = getIntent().getExtras(); 
       if(extras!=null) {
            totalscore = extras.getInt("message");
        }

然后再試一次


查看完整回答
反對 回復(fù) 2023-03-17
?
墨色風(fēng)雨

TA貢獻1853條經(jīng)驗 獲得超6個贊

試試這個例子

<RadioGroup

    android:id="@+id/radioSex"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" >


    <RadioButton

        android:id="@+id/radioMale"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/radio_male" 

        android:checked="true" />


    <RadioButton

        android:id="@+id/radioFemale"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/radio_female" />


</RadioGroup>

在你的A班


radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);

btnDisplay = (Button) findViewById(R.id.btnDisplay);


btnDisplay.setOnClickListener(new OnClickListener() {


    @Override

    public void onClick(View v) {


            // get selected radio button from radioGroup

        int selectedId = radioSexGroup.getCheckedRadioButtonId();


        // find the radiobutton by returned id

            radioSexButton = (RadioButton) findViewById(selectedId);


        Toast.makeText(MyAndroidAppActivity.this,

            radioSexButton.getText(), Toast.LENGTH_SHORT).show();


    }


});

發(fā)布崩潰報告以了解有關(guān)您的問題的更多信息


查看完整回答
反對 回復(fù) 2023-03-17
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號