-3

あるクラスの文字列変数の値を別のクラスで使用したい...

しかし、呼び出されたクラスの呼び出しクラスにオブジェクトを作成してそうすると、値がnullになります。

私はネットとスタックオーバーフローで検索してみましたが、同様の回答をいくつか試しましたが、それらの解決策はそれらの質問に固有のものでした。

ここに2つのクラスがあります-

1つ目は、変数を使用したいクラスです。class is - 別のクラスで使用したい SelectedClass 変数 - SelectedClass( a string)

2クラス目はFEaです。1クラス目の変数「SelectedClass」の値を使いたいです。

CLASS 1(選択クラス)-

package com.attendance_trial.nirmik;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SelectClass extends ListActivity 
{

public String classes2[] = {"FEa","FEb","SEa","SEb"};
String SelectedClass;

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    String SelectedClass = classes2[position];
    try
    {

        Class MyClass = Class.forName("com.attendance_trial.nirmik." + SelectedClass);
        Intent myintent = new Intent(SelectClass.this,MyClass);
        startActivity(myintent);

    }
    catch(ClassNotFoundException cnf)
    {
        cnf.printStackTrace();
    }


}

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    // TODO Auto-generated method stub
    setListAdapter(new ArrayAdapter<String>(SelectClass.this, android.R.layout.simple_list_item_1, classes2));
}   

}

クラス 2 (FEa) -

package com.attendance_trial.nirmik;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FEa extends Activity implements View.OnClickListener
{

SelectClass sc = new SelectClass();

//Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,bSend;
Button bSend;
TextView tvDisp;
String acc="";


@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fea);
    initialise();


}



private void initialise()//initialise all buttons etc
{

    bSend = (Button) findViewById (R.id.BtnSendMsg);
    bSend.setOnClickListener(this);
    tvDisp=(TextView) findViewById (R.id.TxtViewDisplay);

}





public void onButtonClick(View v)
{
    // TODO Auto-generated method stub


        Button theButton = (Button)v;
        String chk = theButton.getText().toString();
        if(chk.contentEquals("1")||chk.contentEquals("2")||chk.contentEquals("3")||chk.contentEquals("4")||chk.contentEquals("5")
                ||chk.contentEquals("6")||chk.contentEquals("7")||chk.contentEquals("8")||chk.contentEquals("9"))
        {
        acc = acc + "0" + chk;
        }
        else
        {
            acc =  acc +chk;
        }
        tvDisp.setText("String Is:" + acc);


}//method end



@Override
public void onClick(View v2) 
{
    // TODO Auto-generated method stub
    int retid = v2.getId();
    if(retid == R.id.BtnSendMsg ) //send button clicked
    {

        String msg = sc.SelectedClass + "2210"+ acc;
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("plain/text");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg);
                startActivity(emailIntent);
    }

}

}//メインエンド

4

2 に答える 2

1

あなたがしていることは完全に間違っています。

を拡張するクラスから手動でオブジェクトを作成することは想定されていませんActivity。別名、これを行うべきではありません: SelectClass sc = new SelectClass();. 次を使用して、Android OSにそれを行うように指示する必要があります。

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

SelectClass から FEa に文字列を送信する場合は、次のような意図で送信できます。

Intent intent = new Intent(this, YourActivity.class);
intent.putExtra(EXTRA_MESSAGE_KEY, yourString);
startActivity(intent);

そして、2 番目のアクティビティの onCreate() で:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the string from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

}

Android でプログラミングする方法については、developer.android.comを確認してください。Android プログラミングは Java プログラミングではないことに注意してください。

ここの開発者サイトから、新しいアクティビティを開始する方法。

于 2012-12-08T14:00:22.680 に答える
-1

を使用して値を渡してみませんreturnか? そうすれば、任意のクラスの任意の関数を呼び出して、値を他のクラスに返すことができます。

戻り値またはクラスの詳細については、こちらを参照してください

于 2012-12-08T13:51:01.623 に答える