0

我收到以下空指针异常。每当我点击按钮(我打算启动)。我得到一个 FC 和这个空指针异常。可能是因为使用了那个文本文件吗?

日志猫

    04-25 20:02:40.762: I/Adreno200-EGLSUB(29671): <ConfigWindowMatch:2081>: Format RGBA_8888.
    04-25 20:02:40.772: D/memalloc(29671): /dev/pmem: Mapped buffer base:0x514e1000 size:3649536 offset:3035136 fd:56
    04-25 20:02:40.982: D/memalloc(29671): /dev/pmem: Mapped buffer base:0x51b08000 size:4325376 offset:3710976 fd:59
    04-25 20:02:41.632: I/Adreno200-EGLSUB(29671): <ConfigWindowMatch:2081>: Format RGBA_8888.
    04-25 20:02:41.632: D/memalloc(29671): /dev/pmem: Mapped buffer base:0x5212e000 size:4939776 offset:4325376 fd:62
    04-25 20:02:41.692: D/memalloc(29671): /dev/pmem: Mapped buffer base:0x52615000 size:5554176 offset:4939776 fd:68
    04-25 20:02:41.712: D/memalloc(29671): /dev/pmem: Unmapping buffer base:0x514e1000 size:3649536 offset:3035136
    04-25 20:02:41.712: D/memalloc(29671): /dev/pmem: Unmapping buffer base:0x51b08000 size:4325376 offset:3710976
    04-25 20:02:42.842: D/memalloc(29671): /dev/pmem: Mapped buffer base:0x513e1000 size:4325376 offset:3710976 fd:53
    04-25 20:02:42.882: W/dalvikvm(29671): threadid=1: thread exiting with uncaught exception (group=0x40dbb9d8)
    04-25 20:02:42.902: E/AndroidRuntime(29671): FATAL EXCEPTION: main
    04-25 20:02:42.902: E/AndroidRuntime(29671): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dreamgoogle.gihf/com.dreamgoogle.gihf.Stories}: java.lang.NullPointerException
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1884)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.os.Handler.dispatchMessage(Handler.java:99)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.os.Looper.loop(Looper.java:137)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.app.ActivityThread.main(ActivityThread.java:4476)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at java.lang.reflect.Method.invokeNative(Native Method)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at java.lang.reflect.Method.invoke(Method.java:511)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at dalvik.system.NativeStart.main(Native Method)
    04-25 20:02:42.902: E/AndroidRuntime(29671): Caused by: java.lang.NullPointerException
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.app.Activity.findViewById(Activity.java:1965)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at com.dreamgoogle.gihf.Stories.<init>(Stories.java:14)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at java.lang.Class.newInstanceImpl(Native Method)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at java.lang.Class.newInstance(Class.java:1319)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.app.Instrumentation.newActivity(Instrumentation.java:1025)
    04-25 20:02:42.902: E/AndroidRuntime(29671):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1875)

xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/jobsdes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/sj1" />

</RelativeLayout>

Java 文件。

    public class Stories extends Activity {
        TextView t1 = (TextView) findViewById(R.id.jobsdes);
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_stories);
            MyPagerAdapter adapter = new MyPagerAdapter();
            ViewPager myPager = (ViewPager) findViewById(R.id.pager1);
            myPager.setAdapter(adapter);
            myPager.setCurrentItem(0);
    
            t1.setText(R.raw.jobs);
    
        }
    
        private class MyPagerAdapter extends PagerAdapter {
            public int getCount() {
                return 4;
            }
    
            public Object instantiateItem(View collection, int position) {
                LayoutInflater inflater = (LayoutInflater) collection.getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                int resId = 0;
                switch (position) {
                case 0:
                    resId = R.layout.activity_stories;
                    break;
                case 1:
                    resId = R.layout.sto2;
                    break;
                case 2:
                    resId = R.layout.sto3;
                    break;
                case 3:
                    resId = R.layout.sto4;
                    break;
                }
                View view = inflater.inflate(resId, null);
                ((ViewPager) collection).addView(view, 0);
                return view;
            }
    
            public void destroyItem(View arg0, int arg1, Object arg2) {
                ((ViewPager) arg0).removeView((View) arg2);
            }
    
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == ((View) arg1);
            }
    
            public Parcelable saveState() {
                return null;
            }
    
        }
    
    }

这是寻呼机 xml 文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Stories" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
4

3 回答 3

4

例外

04-25 20:02:42.902:E/AndroidRuntime(29671):引起:java.lang.NullPointerException 04-25 20:02:42.902:E/AndroidRuntime(29671):在 android.app.Activity.findViewById(Activity .java:1965)

代码

findViewById(R.id.pager1)

XML

android:id="@+id/jobsdes"

我会说,您的 findViewbyId 返回零

于 2013-04-25T14:42:50.297 回答
2

您正在尝试TextView在活动中找到它之前的内容:

public class Stories extends Activity {
    TextView t1 = (TextView) findViewById(R.id.jobsdes); //RIGHT HERE

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stories);

您需要在 之后找到它setContentView,如下所示:

public class Stories extends Activity {
    TextView t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stories);
        t1 = (TextView) findViewById(R.id.josdes);
于 2013-04-25T14:45:54.817 回答
0

在 setcontentview 之后访问 TextView 就可以了。

于 2013-04-28T03:01:35.587 回答