开发者

Cant display data when switching view

开发者 https://www.devze.com 2023-02-13 07:59 出处:网络
I am new to android and needs some help. I am having a problem with the function of switching views. I am developing a Golf application and the program is supposed to take peoples handicaps and pair t

I am new to android and needs some help. I am having a problem with the function of switching views. I am developing a Golf application and the program is supposed to take peoples handicaps and pair them into teams after certain rules.

The program is working just fine, until I implement a function using switched views. The problem occurs when the "user" should fill the handicap of the players in unsorted_teams.xml and then click on a button called next_button and switch view to unsorted_teams2.xml which will display all the possible team-arrangements depending on the data being filled at unsorted_teams.xml.

When I use this view switcher, I keep getting a nullpointerException. When I have everything under the same xml file, its working just fine. So I assume there is nothing wrong with my code, just how I handle the switcher between UnsortedTeams.java, unsorted_teams.xml and UnsortedTeams2.java, unsorted_teams2.xml.

public class UnsortedTeams extends Activity {

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.unsorted_teams);
 }
}

This is the UnsortedTeams java class, nothing interesting so far, so lets move along :)

<?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>

This is the unsorted_teams.xml file which has the next_button who switches the view to unsorted_teams2.xml. Lets move along

Note that I will only include the code that handles the switch:

This is my main java class: ------------>

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

        });
    }}

This is the Class UnsortedTeams2 where the data is supposed to be displayed, but since its a switched view from UnsortedTeams it does not work. The button next_button can be found in my main class higher in the code, which is the button who triggers the "activity". Ive also implemented an activity in the AndroidManifest.xml file.

NOTE that the code works fine for only switching views, but when I try to get ( load ) the data and read it in unsorted_teams2.xml everything crashes as described above. As for now I am not using switched views and run everything under the same xml file, and its working fine. I hope someone understands my problem and have ideas how to solve this. Thx in advance.


EDIT:

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">

This text disappeared from my post, I will just paste it to make everything clear. It was supposed to stand under: "This is the unsorted_teams.xml file which has the next_button who switches the view to unsorted_teams2.xml. Lets move along"

    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>  
0

精彩评论

暂无评论...
验证码 换一张
取 消