2015-05-09 16 views
1

這是運行代碼時加載的默認活動。 當我點擊創建新帳戶鏈接時,屏幕變爲空白並開始出現GC_FOR_ALLOC日誌消息。切換到下一個活動(創建新帳戶頁面)似乎存在問題。我已經在我的索尼xperia z1和geny運動模擬器上試過了,結果也一樣。當點擊鏈接以切換目標時,GC_FOR_ALLOC日誌消息和屏幕變爲空白

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); 
} 
} 

這是我嘗試切換到

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的!

+1

請從logcat添加缺少的消息。它會給人們更多的信息來幫助你。 –

+0

我已經添加了logcat消息 – user3198936

回答

0

唐的使用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(); 
} 
+1

剛試過這個,仍然得到相同的logcat消息 – user3198936

+1

儘量不要在第二個活動中打印數據庫,可能它太大了 –

+0

它現在的作品謝謝! – user3198936