gpt4 book ai didi

java - 单击链接切换 Intent 时 GC_FOR_ALLOC 日志消息和屏幕变为空白

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:46 25 4
gpt4 key购买 nike

这是运行代码时加载的默认 Activity 。当我单击“创建新帐户”链接时,屏幕变为空白,并且开始出现 GC_FOR_ALLOC 日志消息。切换到下一个 Activity (创建新帐户页面)时似乎出现问题。我在我的索尼 xperia z1 和 geny 运动模拟器上都进行了尝试,出现了相同的结果。

package com.michaelsony.gcsemathsrevisionapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.content.Intent;
import android.widget.TextView;


public class LoginPage extends ActionBarActivity {

private static final String TAG = "Michael's Message";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
Log.i(TAG, "OnCreate");
}

public void onClick(View view){
Intent i = new Intent(this, CreateAccount.class);
startActivity(i);
}




@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}


@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}


@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}


@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}


@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart");
}


@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}


@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
}


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login_page, 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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

这是我尝试切换到的 Activity

package com.michaelsony.gcsemathsrevisionapp;

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.EditText;
import android.widget.Toast;
import android.util.Log;


public class CreateAccount extends ActionBarActivity {

EditText firstnamebox, surnamebox, usernamebox, passwordbox, confirmpassbox;
UserDBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
firstnamebox = (EditText) findViewById(R.id.firstnamebox);
surnamebox = (EditText) findViewById(R.id.surnamebox);
usernamebox = (EditText) findViewById(R.id.usernamebox);
passwordbox = (EditText) findViewById(R.id.passwordbox);
confirmpassbox = (EditText) findViewById(R.id.confirmpassbox);
dbHandler = new UserDBHandler(this, null, null, 1);
printDatabase();
}
//Add a user to the database(create a user)
public void createAccountClick(View view){
Users user = new Users(firstnamebox.getText().toString(), surnamebox.getText().toString(), usernamebox.getText().toString(), passwordbox.getText().toString(), confirmpassbox.getText().toString());
Log.d("app1", "Password=" + passwordbox.getText().toString() + " confirm=" + confirmpassbox.getText().toString());
//dbHandler.addUser(user);
//Toast.makeText(getBaseContext(),"Account Created!", Toast.LENGTH_LONG).show();
if(!passwordbox.getText().toString().equals(confirmpassbox.getText().toString()))
{
Toast.makeText(getBaseContext(),"Passwords Do Not Match",Toast.LENGTH_LONG).show() ;
passwordbox.setText("");
confirmpassbox.setText("");
Log.d("app1", "Password=" + passwordbox.getText().toString() + " confirm=" + confirmpassbox.getText().toString());
}
else
{
dbHandler.addUser(user);
Toast.makeText(getBaseContext(),"Account Created!", Toast.LENGTH_LONG).show();
Log.d("app1", "Password=" + passwordbox.getText().toString() + " confirm=" + confirmpassbox.getText().toString());

}



}

public void printDatabase(){
String dbString = dbHandler.databaseToString();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_create_account, 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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

日志消息如下:

05-09 13:34:51.371    1941-1941/com.michaelsony.gcsemathsrevisionapp D/dalvikvm﹕ GC_FOR_ALLOC freed 279K, 12% free 4248K/4776K, paused 5ms, total 5ms
05-09 13:34:51.395 1941-1941/com.michaelsony.gcsemathsrevisionapp D/dalvikvm﹕ GC_FOR_ALLOC freed 155K, 12% free 4216K/4776K, paused 5ms, total 5ms
05-09 13:34:51.399 1941-1941/com.michaelsony.gcsemathsrevisionapp D/dalvikvm﹕ GC_FOR_ALLOC freed 279K, 12% free 4248K/4776K, paused 5ms, total 5ms
05-09 13:34:51.415 1941-1941/com.michaelsony.gcsemathsrevisionapp D/dalvikvm﹕ GC_FOR_ALLOC freed 155K, 12% free 4216K/4776K, paused 14ms, total 14ms
05-09 13:34:51.435 1941-1941/com.michaelsony.gcsemathsrevisionapp D/dalvikvm﹕ GC_FOR_ALLOC freed 279K, 12% free 4248K/4776K, paused 9ms, total 9ms
05-09 13:34:51.447 1941-1941/com.michaelsony.gcsemathsrevisionapp D/dalvikvm﹕ GC_FOR_ALLOC freed 155K, 12% free 4216K/4776K, paused 6ms, total 6ms
05-09 13:34:51.455 1941-1941/com.michaelsony.gcsemathsrevisionapp D/dalvikvm﹕ GC_FOR_ALLOC freed 280K, 12% free 4248K/4776K, paused 6ms, total 6ms

这些只是他们不断充斥 logcat 的一些内容!

最佳答案

不要使用 onClick 作为方法名称,请将其命名为其他名称

public void onClick(View view){
Intent i = new Intent(this, CreateAccount.class);
startActivity(i);
}

喜欢

public void onCreateAccountClicked(View view){
Intent i = new Intent(LoginPage.this, CreateAccount.class);
startActivity(i);
}

并避免打印整个数据库。它可能很大并且占用大量内存,这将导致过度调用 GC(当它开始运行内存不足时),并最终因内存不足而导致应用程序崩溃。

public void printDatabase(){
//String dbString = dbHandler.databaseToString();
}

关于java - 单击链接切换 Intent 时 GC_FOR_ALLOC 日志消息和屏幕变为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30140395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com