viewpager应用程序在textview上的onclicklistener时崩溃

时间:2014-07-17 06:44:34

标签: android

我正在开发一个应用程序,我必须实现一个viewpager,它显示了之前已经赢得过足球世界杯的国家,有多少次以及哪些年份。 在每个国家/地区的页面上,都有一个名为click here的textview。 如果单击此文本视图,将打开另一个活动,在viewpager的此页面上显示国家足球队的维基百科说明。 我编写了代码,但是当我打开应用程序时崩溃了。 有人可以帮我解决吗?

这是我的主要活动代码:

package com.example.countries;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity
{ 
    ViewPager viewPager;
    PagerAdapter adapter;
    String[] times;
    String[] country;
    String[] years;
    String[] page;
    int[] flag;
    TextView txtclick;
    private static int currentPage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        times = new String[] { "2", "5", "1", "1", "4", "4", "1", "2" };

        page = new String[] { "Page 1 of 8", "Page 2 of 8", "Page 3 of 8", "Page 4 of 8",
                "Page 5 of 8" ,"Page 6 of 8", "Page 7 of 8", "Page 8 of 8" }; 

        country = new String[] { "Argentina", "Brazil", "England",
                "France", "Germany", "Italy", "Spain", "Uruguay", };

        years = new String[] { "1978 , 1986", "1958 , 1962 , 1970 , 1994 , 2002",
                "1966", "1998", "1954 , 1974 , 1990 , 2014", "1934 , 1938 , 1982 , 2006",
                "2010", "1930 , 1950" };

        flag = new int[] { R.drawable.flag_of_argentina, R.drawable.flag_of_brazil,
                R.drawable.flag_of_england, R.drawable.flag_of_france,
                R.drawable.flag_of_germany, R.drawable.flag_of_italy, R.drawable.flag_of_spain,
                R.drawable.flag_of_uruguay };

        viewPager = (ViewPager) findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(MainActivity.this, times, country, years, page, flag);
        viewPager.setAdapter(adapter);

        txtclick = (TextView) findViewById(R.id.clickhere);
        txtclick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                PageListener pageListener = new PageListener();
                viewPager.setOnPageChangeListener(pageListener);
                String blogUri = "http://en.wikipedia.org/wiki/" + country[currentPage] + "_national_football_team";
                Intent intent = new Intent(v.getContext(), Wikipedia.class);
                intent.setData(Uri.parse(blogUri));
                startActivity(intent);
            }
        });


    }

    private static class PageListener extends SimpleOnPageChangeListener
    {
        public void onPageSelected(int position) 
        {
               currentPage = position;
        }
    }
}

这是我的viewpager适配器类代码:

package com.example.countries;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewPagerAdapter extends PagerAdapter
{
    Context context;
    String[] times;
    String[] country;
    String[] years;
    String[] page;
    int[] flag;
    LayoutInflater inflater;
    TextView txtclick;
    int pos;

    public ViewPagerAdapter(Context context, String[] times, String[] country,
            String[] years, String[] page, int[] flag) {
        this.context = context;
        this.times = times;
        this.country = country;
        this.years = years;
        this.page = page;
        this.flag = flag;
    }

    @Override
    public int getCount() {
        return times.length;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        TextView txttimes;
        TextView txtcountry;
        TextView txtyears;
        TextView txtpage;
        ImageView imgflag;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_item, container,
                false);

        txttimes = (TextView) itemView.findViewById(R.id.times);
        txtcountry = (TextView) itemView.findViewById(R.id.country);
        txtyears = (TextView) itemView.findViewById(R.id.years);
        txtpage = (TextView) itemView.findViewById(R.id.page);

        txttimes.setText(times[position]);
        txtcountry.setText(country[position]);
        txtyears.setText(years[position]);
        txtpage.setText(page[position]);
        pos = position;

        imgflag = (ImageView) itemView.findViewById(R.id.flag);
        imgflag.setImageResource(flag[position]);

        ((ViewPager) container).addView(itemView);

        /*txtclick = (TextView) itemView.findViewById(R.id.clickhere);
        txtclick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                String blogUri = "http://en.wikipedia.org/wiki/" + country[pos] + "_national_football_team";
                Intent intent = new Intent(context, Wikipedia.class);
                intent.setData(Uri.parse(blogUri));
                context.startActivity(intent);
            }
        });*/

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

这是我的维基百科活动代码:

package com.example.countries;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.webkit.WebView;

public class Wikipedia extends ActionBarActivity {

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

        Intent intent = getIntent();
        Uri blogUri = intent.getData();

        WebView webView = (WebView) findViewById(R.id.webView1);
        webView.loadUrl(blogUri.toString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.wikipedia, menu);
        return true;
    }
}

这是logcat错误:

07-17 09:43:07.353: W/ApplicationPackageManager(10517): getCSCPackageItemText()
07-17 09:43:07.393: E/MoreInfoHPW_ViewGroup(10517): Parent view is not a TextView
07-17 09:43:07.418: D/AndroidRuntime(10517): Shutting down VM
07-17 09:43:07.418: W/dalvikvm(10517): threadid=1: thread exiting with uncaught exception (group=0x41767c08)
07-17 09:43:07.423: E/AndroidRuntime(10517): FATAL EXCEPTION: main
07-17 09:43:07.423: E/AndroidRuntime(10517): Process: com.example.countries, PID: 10517
07-17 09:43:07.423: E/AndroidRuntime(10517): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.countries/com.example.countries.MainActivity}: java.lang.NullPointerException
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.app.ActivityThread.access$800(ActivityThread.java:157)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.os.Looper.loop(Looper.java:157)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.app.ActivityThread.main(ActivityThread.java:5293)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at java.lang.reflect.Method.invoke(Method.java:515)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at dalvik.system.NativeStart.main(Native Method)
07-17 09:43:07.423: E/AndroidRuntime(10517): Caused by: java.lang.NullPointerException
07-17 09:43:07.423: E/AndroidRuntime(10517):    at com.example.countries.MainActivity.onCreate(MainActivity.java:53)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.app.Activity.performCreate(Activity.java:5389)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
07-17 09:43:07.423: E/AndroidRuntime(10517):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
07-17 09:43:07.423: E/AndroidRuntime(10517):    ... 11 more

这是我的activity_main.xml:

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

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

</RelativeLayout>

这是我的viewpager_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:padding="10dp" >

    <TextView
        android:id="@+id/timeslabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/countrylabel"
        android:text="@string/timeslabel" />

    <TextView
        android:id="@+id/times"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/country"
        android:layout_toRightOf="@+id/timeslabel" />

    <TextView
        android:id="@+id/countrylabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/countrylabel" />

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/countrylabel" />

    <TextView
        android:id="@+id/yearslabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timeslabel"
        android:text="@string/yearslabel" />

    <TextView
        android:id="@+id/years"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/times"
        android:layout_toRightOf="@+id/yearslabel" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/yearslabel"
        android:layout_alignParentLeft="true"
        android:text="@string/click" />

    <TextView
        android:id="@+id/clickhere"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/years"
        android:layout_toRightOf="@+id/text"
        android:text="@string/here"
        android:textColor="#398eb5" />

    <ImageView
        android:id="@+id/flag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="1dp" />

    <TextView
        android:id="@+id/page"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

这是我的activity_wikipedia.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"
    tools:context="com.example.countries.Wikipedia$PlaceholderFragment" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

感谢。

3 个答案:

答案 0 :(得分:1)

您的TextView不在actyivity_main.xml文件中。它位于viewpager_item.xml文件中,在View Pager适配器类中膨胀。因此,您需要在适配器类中触发TextView click事件,而不是onCreate()方法。

这将始终为空

    txtclick = (TextView) findViewById(R.id.clickhere);
    txtclick.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            PageListener pageListener = new PageListener();
            viewPager.setOnPageChangeListener(pageListener);
            String blogUri = "http://en.wikipedia.org/wiki/" + country[currentPage] + "_national_football_team";
            Intent intent = new Intent(v.getContext(), Wikipedia.class);
            intent.setData(Uri.parse(blogUri));
            startActivity(intent);
        }
    });

因为TextView不在activity_main.xml文件中。

答案 1 :(得分:0)

问题在于,clickhere textview位于PageViewer内,您无法在MainActivity中使用它。

OnClickListener移到ViewPager课程内。

答案 2 :(得分:0)

(TextView) findViewById(R.id.clickhere):此语句返回null。这可能是因为您的布局未正确初始化或错误地调用R.id.clickhere