我无法理解某事。我有一个单独的 Listener 类 (Android),负责处理 Activity 中的所有听众。
当我设置一个 onclickListener 时:
.setOnClickListener(new ListenerActivity());
它工作正常。但是,我想创建
public ListenerActivity myListener = new ListenerActivity();
并通过以下方式访问它:
SaveButton.setOnClickListener(myListener);
这应该是有意义的,因为我不会每次都创建一个新的监听器 Activity 的实例,而是创建一个新实例一次并重新使用它。但是我得到这个错误:
java.lang.OutOfMemoryError: Failed to allocate a 600680 byte allocation with 4194304 free bytes and 57MB until OOM; failed due to fragmentation (required continguous free 602112 bytes where largest contiguous free 438272 bytes)
at android.util.Log.getStackTraceString(Log.java:343)
at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:61)
at com.android.internal.os.RuntimeInit.-wrap0(RuntimeInit.java)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:86)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
为什么这不起作用?
编辑:
这是我的主课:
public class EditPageMainActivity extends AppCompatActivity {
这是我的监听类:
public class EditJobListenerActivity extends EditJobMainActivity
implements AdapterView.OnItemSelectedListener, AdapterView.OnClickListener,
AdapterView.OnItemClickListener {
最佳答案
您不应该在 Activity 上调用 new,相反,您应该让 Android 操作系统为您做这件事。
SaveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(this, ListenerActivity.class);
startActivity(intent);
}
});
看看 docs有关开始 Activity 的详细信息。
关于java - 声明 Object 的新实例会导致 OutOfMemoryFailure。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32553680/