私はアクティビティから別のアクティビティにメソッドを呼び出す必要があります。私のFirstClassがある:別のアクティビティでメソッドを呼び出す方法
public class firstclass extends Activity {
public String Kind(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean Key = preferences.getBoolean("Key", true);
if(Key){
name="you";
}
else{
name="me";
}
return name;
}
}
secondclassは次のとおりです。
public class secondclass extends Activity {
public void take(String token, int transactionId) {
firstclass first = new firstclass(); //error in this class
first.Kind();
}
}
私のエラーは、次のとおりです。要するに
03-25 19:05:39.082 5421-5487/com.example.com E/AndroidRuntime﹕ FATAL EXCEPTION: pool-5-thread-1
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:197)
at android.os.Handler.<init>(Handler.java:111)
at android.app.Activity.<init>(Activity.java:759)
at com.example.com.firstclass<init>(firstclass.java:17)
at com.example.com.secondclass(secondclass.java:157)
Kind()メソッド実装コードを貼り付けてください。 –
別のクラスのメソッドを使用するには、静的にし、 "firstclass.Kind()"とタイプして呼び出す必要があります。 "firstclass"を初期化してメソッド –
を呼び出す必要はありません。 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);これを静的に "this"を知ることができないようにします。 – Tom