2012-12-03 42 views
0

因此,當我嘗試在模擬器中運行我的代碼時,應用程序背景彈出,然後關閉給我的對話框,「不幸的是,Callisto已停止工作」 我不知道什麼是錯的比它給了我一個空指針異常(49行),但並沒有什麼在線路49不幸的是,Callisto已經停止工作

XML

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/callisto_heading" /> 

<Button 
    android:id="@+id/bClasses" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Classes" 
    android:layout_gravity="center" 
    android:onClick="" 
    /> 

<Button 
    android:id="@+id/bSettings" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Settings" 
    android:layout_gravity="center" 
    android:onClick="" 
/> 

</LinearLayout> 

的Java 包android.callisto.com;

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 

public class CallistoActivity extends Activity { 
/** Called when the activity is first created. */ 
Button settings_button; 
Button classes_button; 
Button home_button; 
CheckBox notif_cb; 
CheckBox math_cb; 
CheckBox science_cb; 
CheckBox ss_cb; 
CheckBox english_cb; 
CheckBox language_cb; 

boolean notif,math,science,english,ss,language; 




@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    //settings layout 
    notif_cb = (CheckBox) findViewById(R.id.cbNotif); 
    math_cb = (CheckBox) findViewById(R.id.cbMath); 
    science_cb = (CheckBox) findViewById(R.id.cbScience); 
    ss_cb = (CheckBox) findViewById(R.id.cbSS); 
    english_cb = (CheckBox) findViewById(R.id.cbEnglish); 
    language_cb = (CheckBox) findViewById(R.id.cbLang); 
    home_button = (Button) findViewById(R.id.bHome); 

    notif = true; 
    math = true; 
    science = true; 
    english = true; 
    ss = true; 
    language = true; 

    home_button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      setContentView(R.layout.main); 

     } 
    });   

     //notifications 

    //main layout 
    settings_button = (Button) findViewById(R.id.bSettings); 
    classes_button = (Button) findViewById(R.id.bClasses); 

    settings_button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      setContentView(R.layout.settings); 
     } 
    }); 
} 

}

FYI應用是加載昨晚..感謝您的幫助,並請記住我是新來的Android編程。謝謝。

+3

哪裏是代碼中的第49行?併發布應用程序崩潰時的logcat結果 –

+0

發佈logcat輸出 – 2012-12-03 06:19:47

+0

對我來說很清楚是什麼導致了這一點,請參閱我的答案。 – NickL

回答

1

的問題是在這裏

home_button = (Button) findViewById(R.id.bHome); 

佈局文件沒有bHome

+0

這確實會導致錯誤,但TS並不理解我猜測的android活動設計模式。看到我的答案更詳細。 – NickL

+0

@NickL我們不是在這裏爲他寫完整的代碼!因爲他需要付出一些努力。 –

+0

你說得對。但由於他的代碼中沒有任何錯誤,除了它在一個活動中,並且他使用setcontentView來更改佈局,我決定拆分代碼(複製/粘貼)。我認爲他仍然無法用你的答案解決問題,因爲他可能不會真正理解爲什麼按鈕不在佈局文件中。據他的理解,這是。 – NickL

0

問題probabaly是,您使用的顯示不同的畫面中的setContentView()。您不應該使用此方法更改屏幕,您應該爲此使用不同的活動。

通常,您只能在oncreate,per activity中設置contentView ONCE。

您的R.layout.main佈局可能不包含'home'按鈕,但是r.layout.settings佈局可以。

首先,您正在加載此行的主佈局:setContentView(R.layout.main);但由於此佈局文件不包含主頁按鈕,因此findViewById(R.id.bHome);將返回null。之後,在你的情況下調用這個返回值的方法home_button.setOnClickListener();將導致NullPointerException。

你應該做的是:

您主要佈局創建活動:

public class CallistoMainActivity extends Activity { 
/** Called when the activity is first created. */ 
Button settings_button; 
Button classes_button; 
Button home_button; 
CheckBox notif_cb; 
CheckBox math_cb; 
CheckBox science_cb; 
CheckBox ss_cb; 
CheckBox english_cb; 
CheckBox language_cb; 

boolean notif,math,science,english,ss,language; 




@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //main layout 
    settings_button = (Button) findViewById(R.id.bSettings); 
    classes_button = (Button) findViewById(R.id.bClasses); 

    settings_button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent(getApplicationContext(), 
        CallistoSettingsActivity.class)); 
     } 
    }); 
} 

併爲您的設置佈局的活動:

public class CallistoSettingsActivity extends Activity { 

Button home_button; 
CheckBox notif_cb; 
CheckBox math_cb; 
CheckBox science_cb; 
CheckBox ss_cb; 
CheckBox english_cb; 
CheckBox language_cb; 

boolean notif,math,science,english,ss,language; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.settings); 


    //settings layout 
    notif_cb = (CheckBox) findViewById(R.id.cbNotif); 
    math_cb = (CheckBox) findViewById(R.id.cbMath); 
    science_cb = (CheckBox) findViewById(R.id.cbScience); 
    ss_cb = (CheckBox) findViewById(R.id.cbSS); 
    english_cb = (CheckBox) findViewById(R.id.cbEnglish); 
    language_cb = (CheckBox) findViewById(R.id.cbLang); 
    home_button = (Button) findViewById(R.id.bHome); 

    notif = true; 
    math = true; 
    science = true; 
    english = true; 
    ss = true; 
    language = true; 

    home_button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      finish(); 
      //You could either use finish() to finish the current activity, and return to the previous CallistoMainActivity on the stack. 
      //Or you could simply start the main activity again by using the startActivity() method you see in the onclicklistener on the settings button. 
     } 
    });   
} 

現在會發生什麼事,是當您單擊主要活動中的設置按鈕時,將顯示設置活動。當您單擊設置活動中的主頁按鈕時,設置活動將完成,並且用戶將返回到堆棧上的上一個活動;這是主要活動。 也別忘了在你的AndroidManifest.xml中定義設置的活動

編輯:另一件你應該注意的事情是,如果在'x行'有一個錯誤,但是你的代碼中沒有任何行x,那麼在您的Android設備上運行的代碼與您在編輯器中查看的代碼不同。所以這可能是它昨晚運行的原因,但現在不再了。

相關問題