我是android新手,需要一些幫助。我遇到了切換視圖功能的問題。我正在開發一款高爾夫應用程序,該程序應該遵循一定的規則,將人們的障礙並將其組合成團隊。切換視圖時不能顯示數據
該程序工作得很好,直到我實現了一個使用切換視圖的函數。當「用戶」在unsorted_teams.xml中填入球員的障礙,然後點擊名爲next_button的按鈕並將視圖切換到unsorted_teams2.xml時,就會出現問題,這將顯示所有可能的團隊安排,具體取決於在unsorted_teams.xml。
當我使用這個視圖切換器時,我總是得到一個nullpointerException。當我擁有同一個xml文件下的所有東西時,它的工作就很好。所以我認爲我的代碼沒有問題,只是我如何處理UnsortedTeams.java,unsorted_teams.xml和UnsortedTeams2.java,unsorted_teams2.xml之間的切換器。
public class UnsortedTeams extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unsorted_teams);
}
}
這是UnsortedTeams java類,沒有什麼有趣的,到目前爲止,所以讓我們一起:)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table2"
android:background="#adaeb5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:weightSum="1.0"
android:gravity="left">
<TableRow
android:gravity="center"
android:background="#adaeb5">
<Button
android:layout_height="wrap_content"
android:layout_width="100px"
android:id="@+id/next_button"
android:text="Submit"></Button>
</TableRow>
</TableLayout>
舉動這是一個有誰切換到unsorted_teams2視圖的next_button的unsorted_teams.xml文件。 XML。讓我們一起
請注意,我將只包括處理開關的代碼移動:
這是我的主要的java類:------------>
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spec = tabHost.newTabSpec("unsorted_teams").setIndicator("UnsortedTeams",
res.getDrawable(R.drawable.ic_tab_images))
.setContent(R.id.table2);
tg = new TeamGenerator(this);
next_button = (Button)findViewById(R.id.next_button);
next_button.setOnClickListener(mNextListener);
private OnClickListener mNextListener = new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), UnsortedTeams2.class);
startActivityForResult(myIntent, 0);
int hcp1 = Integer.parseInt((unsorted_team_hcp1.getText().toString()));
int hcp2 = Integer.parseInt((unsorted_team_hcp2.getText().toString()));
int hcp3 = Integer.parseInt((unsorted_team_hcp3.getText().toString()));
int hcp4 = Integer.parseInt((unsorted_team_hcp4.getText().toString()));
List<Result> result = tg.getTeamResultCombinations(hcp1, hcp2, hcp3, hcp4);
if(result != null)
{
Result r1 = result.get(0);
Result r2 = result.get(1);
Result r3 = result.get(2);
result_1_exp.setText(r1.getExp()+"");
result_1_win.setText(r1.getWin()+"");
result_1_draw.setText(r1.getDraw()+"");
result_1_loss.setText(r1.getLoss()+"");
When the program reads result_1_exp.setText(r1.getExp()+""); this occurs:
02-26 00:17:20.499: ERROR/AndroidRuntime(631): FATAL EXCEPTION: main
02-26 00:17:20.499: ERROR/AndroidRuntime(631): java.lang.NullPointerException
02-26 00:17:20.499: ERROR/AndroidRuntime(631): at com.tiebreaker.gui.tiebreaker$1.onClick(tiebreaker.java:195)
public class UnsortedTeams2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unsorted_teams2);
Button next = (Button) findViewById(R.id.submit3_button);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}}
這是數據應該顯示的Class UnsortedTeams2,但是由於它是從UnsortedTeams切換的視圖,它不起作用。按鈕next_button可以在我的主類中的代碼中找到,這是觸發「活動」的按鈕。我也在AndroidManifest.xml文件中實現了一個活動。
請注意,該代碼只適用於切換視圖,但是當我嘗試獲取(加載)數據並在unsorted_teams2.xml中讀取它時,如上所述崩潰。至於現在我沒有使用切換的視圖,並在同一個XML文件下運行的一切,它的工作正常。我希望有人瞭解我的問題,並有想法如何解決這個問題。 Thx提前。
歡迎的StackOverflow!你應該給問題添加標籤以包含你的系統,編程語言等等(至少包括Android!)。 :-) – 2011-02-26 02:06:27