我启动应用后,下面的代码崩溃了。我想用我的" greenBackground"和" whiteBackground" TestActivity的setOnClickListener方法中Methods.class的方法。有没有一种方法从Methods类调用方法?任何帮助表示赞赏。
TestActivity.class:
package com.mfg.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class TestActivity extends ActionBarActivity {
Methods method = new Methods();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.program);
Button greenButton = (Button) findViewById(R.id.GreenButton);
Button whiteButton = (Button) findViewById(R.id.WhiteButton);
greenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
method.greenBackground(v);
}
});
whiteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
method.whiteBackground(v);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Methods.class:
package com.mfg.test;
import android.graphics.Color;
import android.view.View;
public class Methods extends TestActivity {
View rl = findViewById(R.id.rl);
public void greenBackground(View rl)
{
rl.setBackgroundColor(Color.GREEN);
}
public void whiteBackground(View rl)
{
rl.setBackgroundColor(Color.WHITE);
}
}
以下是日志文件:
07-29 10:42:37.962: E/AndroidRuntime(2017): FATAL EXCEPTION: main
07-29 10:42:37.962: E/AndroidRuntime(2017): Process: com.mfg.test, PID: 2017
07-29 10:42:37.962: E/AndroidRuntime(2017): java.lang.StackOverflowError
07-29 10:42:37.962: E/AndroidRuntime(2017): at java.lang.ref.FinalizerReference.<init>(FinalizerReference.java:42)
07-29 10:42:37.962: E/AndroidRuntime(2017): at java.lang.ref.FinalizerReference.add(FinalizerReference.java:54)
07-29 10:42:37.962: E/AndroidRuntime(2017): at android.os.StrictMode$InstanceTracker.<init>(StrictMode.java:2244)
07-29 10:42:37.962: E/AndroidRuntime(2017): at android.os.StrictMode.trackActivity(StrictMode.java:1946)
07-29 10:42:37.962: E/AndroidRuntime(2017): at android.app.Activity.<init>(Activity.java:761)
07-29 10:42:37.962: E/AndroidRuntime(2017): at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:76)
07-29 10:42:37.962: E/AndroidRuntime(2017): at android.support.v7.app.ActionBarActivity.<init>(ActionBarActivity.java:55)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:11)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>(Methods.java:6)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.TestActivity.<init>(TestActivity.java:13)
07-29 10:42:37.962: E/AndroidRuntime(2017): at com.mfg.test.Methods.<init>
似乎&#34;方法方法=新方法();&#34; TestActivity类中的行。我该如何解决这个问题?
答案 0 :(得分:3)
在setContentView(R.layout.program);
之后的onCreate方法中获取Id:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.program);
Button greenButton = (Button) findViewById(R.id.GreenButton);
Button whiteButton = (Button) findViewById(R.id.WhiteButton);
more code goes here..
请不要忘记在Android清单文件中提及您的方法活动:
赞<activity android:name=".Methods" />
答案 1 :(得分:0)
我不明白你想做什么,但这似乎是一个非常糟糕的结构。发生StackOverflowException是因为你的程序变得无限递归这两个类。你真的需要方法活动吗?
greenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View greenButton) {
greenButton.setBackgroundColor(Color.GREEN);
}
});
该侦听器中的View参数是greenButton本身,因此您可以使用它并设置背景颜色。
<强>解释强>
你不需要Activity,你可以像以下那样制作普通类:
public class Methods{
public void greenBackground(View rl) {
rl.setBackgroundColor(Color.GREEN);
}
public void whiteBackground(View rl) {
rl.setBackgroundColor(Color.WHITE);
}
}
在您的活动中创建一个实例,并在没有不必要的引用的情况下使用它。