0

这是我的代码

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

public class CurrentActivity extends InfoActivity {

    private ViewPager columnas;
    private static int NUM_COLUMNAS = 4;
    private Context cxt;
    private ColumnAdapter miAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current);
        info();
        cxt = this;

        miAdapter = new ColumnAdapter();
        columnas = (ViewPager) findViewById(R.id.columnas);
        columnas.setAdapter(miAdapter);

        TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
        titleIndicator.setViewPager(columnas);

    }

    private class ColumnAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return NUM_COLUMNAS;
        }

        @Override
        public Object instantiateItem(View collection, int position) {
            ScrollView v = (ScrollView) LayoutInflater.from(cxt).inflate(
                    R.layout.info, null);

            if (position == 0) {
                v = (ScrollView) LayoutInflater.from(cxt).inflate(
                        R.layout.info, null);

            } else if (position == 1) {
                v = (ScrollView) LayoutInflater.from(cxt).inflate(
                        R.layout.look, null);
            } else if (position == 2) {
                v = (ScrollView) LayoutInflater.from(cxt).inflate(
                        R.layout.touch, null);
            } else if (position == 3) {
                v = (ScrollView) LayoutInflater.from(cxt).inflate(
                        R.layout.tilt, null);
            }

            ((ViewPager) collection).addView(v, 0);

            return v;
        }

        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((ScrollView) view);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ScrollView) object);
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {

        }

        @Override
        public CharSequence getPageTitle(int position) {
            String a = null;
            TextView info = (TextView) findViewById(R.id.info_textview_info);
            TextView look = (TextView) findViewById(R.id.info_textview_look);
            TextView touch = (TextView) findViewById(R.id.info_textview_touch);
            TextView tilt = (TextView) findViewById(R.id.info_textview_tilt);

            if (position == 0) {
                a = getResources().getString(R.string.info).toUpperCase();
                info.setText((R.string.one_lev_info));
                info.setGravity(Gravity.LEFT);
                info.setTextSize(15);

            } else if (position == 1) {
                a = getResources().getString(R.string.look).toUpperCase();
                look.setText((R.string.one_lev_look));
                look.setGravity(Gravity.LEFT);
                look.setTextSize(15);

            } else if (position == 2) {
                a = getResources().getString(R.string.touch).toUpperCase();
                touch.setText((R.string.one_lev_look));
                touch.setGravity(Gravity.LEFT);
                touch.setTextSize(15);

            } else if (position == 3) {
                a = getResources().getString(R.string.tilt).toUpperCase();
                tilt.setText((R.string.one_lev_look));
                tilt.setGravity(Gravity.LEFT);
                tilt.setTextSize(15);

            }
            return a;

        }

    }

好的,这是我的信息布局,例如:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <TextView
        android:id="@+id/info_textview_look"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/look_no"
        android:textColor="#4D4D4D"
        android:textSize="25dp"
        android:textStyle="normal" />

</ScrollView>

这是带有文本的 XML:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="one_lev_info">The shows a 1789</string>
    <string name="one_lev_look">consisting of rhomboid</string>

</resources>

但是当我运行它时,它显示了java文件中的错误,在函数getPageTitle中,就行了

touch.setText((R.string.one_lev_look));

但是当我删除与 Textview 触摸和倾斜相关的所有内容时,没关系。所以我试图只有 3 列,但它仍然只存在第三列的问题。也许我错了。

这是错误日志

07-30 08:40:09.204: E/AndroidRuntime(9115): FATAL EXCEPTION: main
07-30 08:40:09.204: E/AndroidRuntime(9115): java.lang.NullPointerException
07-30 08:40:09.204: E/AndroidRuntime(9115):     at my.package.oh.yes.CurrentActivity$ColumnAdapter.getPageTitle(CurrentActivity.java:110)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at com.viewpagerindicator.TitlePageIndicator.getTitle(TitlePageIndicator.java:802)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at com.viewpagerindicator.TitlePageIndicator.calcBounds(TitlePageIndicator.java:639)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at com.viewpagerindicator.TitlePageIndicator.calculateAllBounds(TitlePageIndicator.java:616)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at com.viewpagerindicator.TitlePageIndicator.onDraw(TitlePageIndicator.java:336)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.View.draw(View.java:6970)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.View.draw(View.java:6973)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.View.draw(View.java:6973)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.View.draw(View.java:6973)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1997)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewRoot.draw(ViewRoot.java:1600)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1321)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.os.Looper.loop(Looper.java:150)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at android.app.ActivityThread.main(ActivityThread.java:4277)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at java.lang.reflect.Method.invoke(Method.java:507)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-30 08:40:09.204: E/AndroidRuntime(9115):     at dalvik.system.NativeStart.main(Native Method)

你有什么建议?

4

1 回答 1

0

您的布局中没有 ID: 的 TextView info_textview_touch。因此,此方法findViewById(R.id.info_textview_touch);将返回 null 给您。

当你尝试打电话时

touch.setText((R.string.one_lev_look));

你会得到一个 nullPointerException 因为对象touch是空的。

如果您希望使用其他 TextView,则需要将它们添加到您的布局 xml 文件中。

于 2012-07-29T21:09:17.917 回答