0

我正在开发我的第一个 Android 应用程序。当我在 OnCreate 和 onRestart 上添加保存和加载对象和设置的功能时,应用程序在启动时开始崩溃。

这是我为设置事物而编写的代码

public void setSettings() {
    SharedPreferences settings = getSharedPreferences("Prefs", 0);
    newGame = settings.getBoolean("newGame", true);
    autoDecide = settings.getBoolean("autoDecide", true);
    saved = settings.getBoolean("saved", false);

    if (saved) {
        player1 = readPlayer("Player 1");
        player2 = readPlayer("Player 2");
        if (player1 != null && player2 != null) {
            ((TextView) findViewById(R.id.Player1name)).setText("" + player1.name);
            ((TextView) findViewById(R.id.Player2name)).setText("" + player2.name);
            ((TextView) findViewById(R.id.player1wins)).setText("" + player1.totalWins);
            ((TextView) findViewById(R.id.player2wins)).setText("" + player2.totalWins);
            ((TextView) findViewById(R.id.Player1life)).setText("" + player1.life);
            ((TextView) findViewById(R.id.Player2life)).setText("" + player2.life);
        }

    }

logCat 显示的 NullPointerException 指向第一个 findViewByID(R.id.Player1name)。我尝试只传入一个普通的字符串,但仍然得到一个 nullPointer,所以我知道它是 findViewById。

这是调用它的 OnCreate

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ViewPagerAdapter adapter= new ViewPagerAdapter();
    ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
    TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicator);
    pager.setAdapter(adapter);
    indicator.setViewPager(pager);
    pager.setCurrentItem(1);
    setSettings();

} 

我的应用程序使用 ViewPagerIndicator,因此有人找不到它吗?

这是代码的主要部分使用 ViewPager 并切换视图。

public class ViewPagerAdapter extends PagerAdapter implements TitleProvider{
private static String[] titles = new String[]{"Settings", "2 Players"};

@Override
public int getCount() {
    return titles.length;
}
public Object instantiateItem(View collection, int position){
    LayoutInflater inflator = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    int resID = 0;
    switch (position){
    case 0:
        resID = R.layout.settings;
        break;
    case 1:
        resID = R.layout.two_player;
        break;
    }
    View view = inflator.inflate(resID, null);
    ((ViewPager)collection).addView(view, 0);
    return view;
}

这是首次创建时应使用的布局。

### Layout XML ###
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
        android:id="@+id/Player1name"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="35dp"
        android:text="@string/player1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/Player2name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight = "34dp"
        android:layout_marginTop="35dp"
        android:text="@string/player2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/Player1life"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Player1name"
        android:layout_below="@+id/Player1name"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="16dp"
        android:text="@string/player1life"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="28sp" />


    <TextView
        android:id="@+id/Player2life"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/player1add"
        android:layout_alignRight="@+id/Player2name"
        android:text="@string/player2life"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="28sp" />        

    <TableLayout
        android:id="@+id/player1add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Player1life"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="16dp" >

        <TableRow
            android:id="@+id/p1add1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/player1add1000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player1add1000"
                android:text="@string/add1000" />

            <Button
                android:id="@+id/player1add500"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player1add500"
                android:text="@string/add500" />
        </TableRow>

        <TableRow
            android:id="@+id/p1add2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/player1add100"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player1add100"
                android:text="@string/add100" />

            <Button
                android:id="@+id/player1add50"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player1add50"
                android:text="@string/add50" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/player2add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/player1add"
        android:layout_alignParentRight = "true"
        android:layout_marginRight = "15dp" >

        <TableRow
            android:id="@+id/p2add1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/player2add1000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player2add1000"
                android:text="@string/add1000" />

            <Button
                android:id="@+id/player2add500"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player2add500"
                android:text="@string/add500" />
        </TableRow>

        <TableRow
            android:id="@+id/p2add2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/player2add100"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player2add100"
                android:text="@string/add100" />

            <Button
                android:id="@+id/player2add50"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player2add50"
                android:text="@string/add50" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/player1sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/player1add"
        android:layout_marginTop="133dp" 
        android:layout_marginLeft="16dp">


        <TableRow
            android:id="@+id/p1sub1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/player1sub1000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player1sub1000"
                android:text="@string/sub1000" />

            <Button
                android:id="@+id/player1sub500"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player1sub500"
                android:text="@string/sub500" />
        </TableRow>

        <TableRow
            android:id="@+id/p1sub2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/player1sub100"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player1sub100"
                android:text="@string/sub100" />

            <Button
                android:id="@+id/player1sub50"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player1sub50"
                android:text="@string/sub50" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/player2sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/player1sub"
        android:layout_alignParentRight = "true"
        android:layout_marginRight =  "15dp">


        <TableRow
            android:id="@+id/p2sub1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/player2sub1000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player2sub1000"
                android:text="@string/sub1000" />

            <Button
                android:id="@+id/player2sub500"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player2sub500"
                android:text="@string/sub500" />
        </TableRow>

        <TableRow
            android:id="@+id/p2sub2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/player2sub100"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player2sub100"
                android:text="@string/sub100" />

            <Button
                android:id="@+id/player2sub50"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="Player2sub50"
                android:text="@string/sub50" />
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/player1sub"
        android:layout_marginTop="30dp"
        android:layout_centerInParent="true"
        android:onClick="reset"
        android:text="@string/reset" />

我检查了一下,但我确实设置了 setContentView 并尝试清理项目,所以我不知道可能出了什么问题。

4

1 回答 1

0

如下更新您的onCreate(Bundle savedInstanceState)方法,它可能会对您有所帮助

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ViewPagerAdapter adapter= new ViewPagerAdapter();
        ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
        TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicator);
        pager.setAdapter(adapter);
        indicator.setViewPager(pager);
        pager.setCurrentItem(1);

        // added by subodh 
        TextView txtView1 = ((TextView) findViewById(R.id.Player1name));
        TextView txtView2 = ((TextView) findViewById(R.id.Player2name));
        TextView txtView3 = ((TextView) findViewById(R.id.player1wins));
        TextView txtView4 = ((TextView) findViewById(R.id.player2wins));
        TextView txtView5 = ((TextView) findViewById(R.id.Player1life));
        TextView txtView6 = ((TextView) findViewById(R.id.Player2life));

        setSettings(txtView1,txtView2,txtView3,txtView4,txtView5,txtView6);

    }

编辑您的第二种方法:

public void setSettings(TextView txtView1,TextView txtView2,TextView txtView3,TextView txtView4,TextView txtView5,TextView txtView6) {
        SharedPreferences settings = getSharedPreferences("Prefs", 0);
        newGame = settings.getBoolean("newGame", true);
        autoDecide = settings.getBoolean("autoDecide", true);
        saved = settings.getBoolean("saved", false);

        if (saved) {
            player1 = readPlayer("Player 1");
            player2 = readPlayer("Player 2");
            if (player1 != null && player2 != null) 
            {
                txtView1.setText("" + player1.name);
                txtView2.setText("" + player2.name);
                txtView3.setText("" + player1.totalWins);
                txtView4.setText("" + player2.totalWins);
                txtView5.setText("" + player1.life);
                txtView6.setText("" + player2.life);


            }

        }

}
于 2012-07-06T06:21:58.047 回答