という名前のアクティビティクラスがあると仮定しMainActivity.java
ます。ただし、このアクティビティには、たとえば約3000行のコードがあります。
このファイルのコード部分を。という名前の外部Javaファイル(クラス)に分離したいと思いますNecessaryThings.java
。しかし、エミュレーターでプロジェクトを実行すると、この操作の後でプロジェクトが停止します。
この活動のいくつかの行を分離する方法はありますか?
私はより良いミニ例を書きました。
また、あなたはどう思いますか。
この方法を使用することは、パフォーマンスの観点から有益または有害ですか?
これは私のMainActivity.javaです
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//I want to call these lines from NecessaryThings.java
TextView genderResult = (TextView) findViewById(R.id.genderText);
genderResult.setText("Cinsiyet:");
TextView calorieResult = (TextView) findViewById(R.id.remainCalorie);
String getGenderSTR = getIntent().getStringExtra("GENDER");
genderResult.setText(getGenderSTR);
String calorieResultSTR = getIntent().getStringExtra("CALORIECHOOSED");
calorieResult.setText(calorieResultSTR);
/*
.....
.....
*/
}
Aftet上記のコードを取得し、それをに保存したいNecessaryThings.java
このような..
//All necessary imports here. There is no problem about those.
public class NecessaryThings extends Activity {
public void myPersonalMethod() {
TextView genderResult = (TextView) findViewById(R.id.genderText);
genderResult.setText("Cinsiyet:");
TextView calorieResult = (TextView) findViewById(R.id.remainCalorie);
String getGenderSTR = getIntent().getStringExtra("GENDER");
genderResult.setText(getGenderSTR);
String calorieResultSTR = getIntent().getStringExtra("CALORIECHOOSED");
calorieResult.setText(calorieResultSTR);
}
}
MainActivity.javaを再配置すると、次のようになります...
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NecessaryThings showMyMethod = new NecassaryThings();
showMyMethod.myPersonalMethod();
/*
the rest of the codes...
*/
}
しかし、コードを分離すると機能しません。なぜ、どうすればそれができますか?