本文介绍了Android的链接适配器崩溃错误:显示java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到错误显示java.lang.NullPointerException,我不知道该怎么做或如何解决它,可能会有人请帮助我。

我很愿意帮助我尽快想这之前,我回学校,所以我不失败的考试结束。 :P

我有3个片段创建了一个tabhost和片段内早餐的旅馆将所有项目的列表视图。

MainActivity

 进口android.app.ActionBar;
进口android.app.FragmentTransaction;
进口android.content.Context;
进口android.content.res.Resources;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.view.ViewPager;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.TextView;

进口的java.util.ArrayList;

进口tk.delacour.tabdemo.fragments.FragmentB;
进口tk.delacour.tabdemo.fragments.FragmentPageAdapter;


公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener {

    动作条动作条;
    ViewPager viewPager;
    FragmentPageAdapter英尺;
    私人捆绑savedInstanceState;

    ListView控件的ListView;
    FragmentB baseAdapter;

    @覆盖
    保护无效的onCreate(包savedInstanceState){

            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);

            viewPager =(ViewPager)findViewById(R.id.pager);
            英尺=新FragmentPageAdapter(getSupportFragmentManager());
            viewPager.setAdapter(英尺);
            动作条= getActionBar();
            断言动作条!= NULL;
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            actionBar.addTab(actionBar.newTab()的setText(家)setTabListener(本)。);
            actionBar.addTab(actionBar.newTab()的setText(商店)setTabListener(本)。);
            actionBar.addTab(actionBar.newTab()的setText(联系方式)setTabListener(本)。);

            ListView的=(的ListView)findViewById(R.id.itemListView);
            listView.setAdapter(新的适配器(本)); //错误这里.....



            viewPager.setOnPageChangeListener(新ViewPager.OnPageChangeListener(){
                @覆盖
                公共无效onPageScrolled(INT I,浮动V,INT I2){

                }

                @覆盖
                公共无效onPageSelected(int i)以{


                    actionBar.setSelectedNavigationItem(ⅰ);


                    }

                @覆盖
                公共无效onPageScrollStateChanged(int i)以{

                }



            });

        }

    @覆盖
    公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction英尺){
        viewPager.setCurrentItem(tab.getPosition());

    }

    @覆盖
    公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction英尺){

    }

    @覆盖
    公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction英尺){

    }




}

类SingleRow {
    字符串codeREF;
    字符串称号;
    字符串的价格;
    字符串描述;
    INT图像;
    SingleRow(字符串codeREF,标题字符串,字符串的价格,字符串描述,诠释图像){
        这codeREF = codeREF。
        this.images =图像;
        this.title =称号;
        this.price =价格;
        this.description =描述;


    }
}

类适配器扩展了BaseAdapter {

    ArrayList的< SingleRow> ArrayList的;
    上下文语境;

    适配器(上下文contxt){
        上下文= contxt;
        ArrayList的=新的ArrayList< SingleRow>();

        资源RES = contxt.getResources();
        的String [] codeREF = res.getStringArray(R.array codeREF);
        的String []标题= res.getStringArray(R.array.title);
        的String []价格= res.getStringArray(R.array.price);
        的String []说明= res.getStringArray(R.array.description);
        INT []的图像= {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6,R.drawable.pic7,R。 drawable.pic8,R.drawable.pic9,R.drawable.pic10,R.drawable.pic11,R.drawable.pic12,R.drawable.pic13,R.drawable.pic14,};

        的for(int i = 0; I< 14;我++){
            arrayList.add(新SingleRow(codeREF [我],标题[I],价格[I],说明[I],图像[I]));
        }

    }

    @覆盖
    公众诠释getCount将(){
        返回arrayList.size();
    }

    @覆盖
    公共对象的getItem(INT位置){
        返回arrayList.get(位置);
    }

    @覆盖
    众长getItemId(INT位置){
        返回0;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看排= inflater.inflate(R.layout.item_layout,父母,假);
        TextView的标题=(TextView中)row.findViewById(R.id.txtTitle);
        TextView的价格=(TextView中)row.findViewById(R.id.txtPrice);
        TextView的说明=(TextView中)row.findViewById(R.id.txtDescription);
        ImageView的形象=(ImageView的)row.findViewById(R.id.imageView);

        SingleRow温度= arrayList.get(位置);

        title.setText(temp.title);
        price.setText(temp.price);
        description.setText(temp.description);
        image.setImageResource(temp.images);

        返回行;
    }
}
 

片段A B和C

 进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;

进口tk.delacour.tabdemo.R;

/ **
 *在2014年5月10号创建于基督徒。
 * /
公共类碎裂扩展片段{

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.fragment_a,集装箱,假);
    }
}


进口android.support.v4.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;

进口tk.delacour.tabdemo.R;

/ **
 *在2014年5月10号创建于基督徒。
 * /
公共类FragmentB扩展片段{

    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.fragment_b,集装箱,假);


    }



进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;

进口tk.delacour.tabdemo.R;

/ **
 *在2014年5月10号创建于基督徒。
 * /
公共类FragmentC扩展片段{

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.fragment_c,集装箱,假);
    }


}
 

FragmentPageAdapter

 进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;

/ **
 *在2014年5月10号创建于基督徒。
 * /
公共类FragmentPageAdapter扩展FragmentPagerAdapter {

    公共FragmentPageAdapter(FragmentManager FM){
        超(FM);
    }

    @覆盖
    公共片段的getItem(INT为arg0){
        开关(将arg0){
            情况下0:
                返回新碎裂();
            情况1:
                返回新FragmentB();
            案例2:
                返回新FragmentC();
            默认:
                打破;
        }
        返回null;
    }

    @覆盖
    公众诠释getCount将(){

        返回3;
    }
}
 

logcat的

  10-10 21:10:58.502 19591-19591 / tk.delacour.tabdemo D / AndroidRuntime:关闭虚拟机
10-10 21:10:58.502 19591-19591 / tk.delacour.tabdemo W / dalvikvm:主题ID = 1:螺纹未捕获异常退出(组= 0x42f05500)
10-10 21:10:58.502 19591-19591 / tk.delacour.tabdemo E / AndroidRuntime:致命异常:主要
    java.lang.RuntimeException的:无法启动的活动ComponentInfo {tk.delacour.tabdemo / tk.delacour.tabdemo.MainActivity}:显示java.lang.NullPointerException
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
            在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
            在android.app.ActivityThread.access $ 600(ActivityThread.java:148)
            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1355)
            在android.os.Handler.dispatchMessage(Handler.java:99)
            在android.os.Looper.loop(Looper.java:150)
            在android.app.ActivityThread.main(ActivityThread.java:5390)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:525)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            在dalvik.system.NativeStart.main(本机方法)
     显示java.lang.NullPointerException:产生的原因
            在tk.delacour.tabdemo.MainActivity.onCreate(MainActivity.java:52)
            在android.app.Activity.performCreate(Activity.java:5303)
            在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
在android.app.ActivityThread.access $ 600(ActivityThread.java:148)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1355)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:150)
在android.app.ActivityThread.main(ActivityThread.java:5390)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:525)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
在dalvik.system.NativeStart.main(本机方法)
 

解决方案

我觉得列表视图应该在片段公共类FragmentB扩展片段{

 公开查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    视图V = inflater.inflate(R.layout.fragment_b,集装箱,假);
    列表视图=(ListView控件)v.findViewById ....
   返回伏;
}
 

......}在fragmentB创建适配器,使用列表视图setAdatper。

I get the error java.lang.NullPointerException, and i have no clue what to do or how to fix it, may some one please assist me.

I would love help asap I want to finish this before i go back to school so i don't fail exams. :P

I have 3 fragments creating up a tabhost, and inside fragment b there will be the list view of all the items.

MainActivity

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import tk.delacour.tabdemo.fragments.FragmentB;
import tk.delacour.tabdemo.fragments.FragmentPageAdapter;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    ActionBar actionBar;
    ViewPager viewPager;
    FragmentPageAdapter ft;
    private Bundle savedInstanceState;

    ListView listView;
    FragmentB baseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            viewPager = (ViewPager) findViewById(R.id.pager);
            ft = new FragmentPageAdapter(getSupportFragmentManager());
            viewPager.setAdapter(ft);
            actionBar = getActionBar();
            assert actionBar != null;
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            actionBar.addTab(actionBar.newTab().setText("Home").setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("Shop").setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("Contact").setTabListener(this));

            listView = (ListView) findViewById(R.id.itemListView);
            listView.setAdapter(new Adapter(this)); //Error Here.....



            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i2) {

                }

                @Override
                public void onPageSelected(int i) {


                    actionBar.setSelectedNavigationItem(i);


                    }

                @Override
                public void onPageScrollStateChanged(int i) {

                }



            });

        }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }




}

class SingleRow {
    String codeRef;
    String title;
    String price;
    String description;
    int images;
    SingleRow(String codeRef, String title, String price, String description, int images) {
        this.codeRef = codeRef;
        this.images = images;
        this.title = title;
        this.price = price;
        this.description = description;


    }
}

class Adapter extends BaseAdapter {

    ArrayList<SingleRow> arrayList;
    Context context;

    Adapter(Context contxt) {
        context = contxt;
        arrayList = new ArrayList<SingleRow>();

        Resources res = contxt.getResources();
        String[] codeRef = res.getStringArray(R.array.codeRef);
        String[] title = res.getStringArray(R.array.title);
        String[] price = res.getStringArray(R.array.price);
        String[] descriptions = res.getStringArray(R.array.description);
        int[] images = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9, R.drawable.pic10, R.drawable.pic11, R.drawable.pic12, R.drawable.pic13, R.drawable.pic14,};

        for (int i = 0; i < 14; i++) {
            arrayList.add(new SingleRow(codeRef[i], title[i], price[i], descriptions[i], images[i]));
        }

    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.item_layout, parent, false);
        TextView title = (TextView) row.findViewById(R.id.txtTitle);
        TextView price = (TextView) row.findViewById(R.id.txtPrice);
        TextView description = (TextView) row.findViewById(R.id.txtDescription);
        ImageView image = (ImageView) row.findViewById(R.id.imageView);

        SingleRow temp = arrayList.get(position);

        title.setText(temp.title);
        price.setText(temp.price);
        description.setText(temp.description);
        image.setImageResource(temp.images);

        return row;
    }
}

Fragment A B and C

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import tk.delacour.tabdemo.R;

/**
 * Created by Christian on 05/10/2014.
 */
public class FragmentA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }
}


import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import tk.delacour.tabdemo.R;

/**
 * Created by Christian on 05/10/2014.
 */
public class FragmentB extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_b, container, false);


    }



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import tk.delacour.tabdemo.R;

/**
 * Created by Christian on 05/10/2014.
 */
public class FragmentC extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_c, container, false);
    }


}

FragmentPageAdapter

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by Christian on 05/10/2014.
 */
public class FragmentPageAdapter extends FragmentPagerAdapter {

    public FragmentPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        switch (arg0){
            case 0:
                return new FragmentA();
            case 1:
                return new FragmentB();
            case 2:
                return new FragmentC();
            default:
                break;
        }
        return null;
    }

    @Override
    public int getCount() {

        return 3;
    }
}

LOGCAT

10-10 21:10:58.502  19591-19591/tk.delacour.tabdemo D/AndroidRuntime﹕ Shutting down VM
10-10 21:10:58.502  19591-19591/tk.delacour.tabdemo W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x42f05500)
10-10 21:10:58.502  19591-19591/tk.delacour.tabdemo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.delacour.tabdemo/tk.delacour.tabdemo.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
            at android.app.ActivityThread.access$600(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:150)
            at android.app.ActivityThread.main(ActivityThread.java:5390)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at tk.delacour.tabdemo.MainActivity.onCreate(MainActivity.java:52)
            at android.app.Activity.performCreate(Activity.java:5303)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
            at android.app.ActivityThread.access$600(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:150)
            at android.app.ActivityThread.main(ActivityThread.java:5390)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
解决方案

I think listview should be in fragmentpublic class FragmentB extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_b, container, false);
    listview = (ListView)v.findViewById....
   return v;
}

......}create adapter in fragmentB, use listview setAdatper.

这篇关于Android的链接适配器崩溃错误:显示java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:34