我能做些什么这个空指针异常? [英] what can i do about this null pointer exception?

查看:127
本文介绍了我能做些什么这个空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类显示扩展ListActivity
{
    私人ForumAdapter faHelper;
    私人ProgressDialog对话框;
    私人字符串URL;
    私人INT成功= 0;    私人SeparatedListAdapter sAdapter;
    私人ListView控件列表;    私人列表<地图<字符串,>> cat_list;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setTitle(「董事会指数);        名单=(ListView控件)findViewById(R.id.dlist);
        list.setOnItemClickListener(itemclicklistener());        的setContentView(R.layout.displaylist);        faHelper =新ForumAdapter(本);        URL =的getURL(savedInstanceState);        如果(faHelper.isGood(URL))
        {
            加载();
        }
        其他
        {
            alertdialog(0);
        }
    }    私有类PBAR扩展的AsyncTask<弦乐,太虚,整数GT;
    {
        私人最终字符串消息=Loading请稍候......;        @覆盖
        在preExecute保护无效()
        {
            对话框= ProgressDialog.show(Display.this,消息,真正的);
        }        @覆盖
        保护无效onPostExecute(整数结果)
        {
            成功=结果;
            dialog.dismiss();
            displaystuff();
        }        保护整数doInBackground(字符串... URL)
        {
            返回faHelper.SourceDownload(URL [0]);
        }
    }    私人无效displaystuff()
    {
        如果(成功== 1)
        {
            sAdapter =新SeparatedListAdapter(这一点,R.layout.demoheader);            对于(对象行:faHelper.BoardIndex)
            {
                的String [] [] = theRow(字符串[] [])行;
                串ctitle =;
                cat_list =新的LinkedList<地图<字符串,>>();                的for(int i = 0; I< theRow.length;我++)
                {
                    如果(ⅰ== 0)ctitle = theRow [I] [0];                    / * +\\ N+ theRow [I] [2] * /
                    cat_list.add(的CreateObject(theRow [I] [1],theRow [Ⅰ] [3]));
                }                ExAdapter exone =新ExAdapter(这一点,R.layout.demorow,cat_list);
                sAdapter.addSection(ctitle,exone);
            }
            list.setAdapter(sAdapter);
        }
        其他
        {
            alertdialog(1);
        }
    }    私人OnItemClickListener itemclicklistener()
    {
        返回新OnItemClickListener()
        {
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的ID)
            {
                @燮pressWarnings(未登记)// Object对地图。
                地图<字符串,> 1 =(地图<弦乐,>?)。parent.getAdapter()的getItem(位置);
                Toast.makeText(getApplicationContext(),one.get(题)的toString(),Toast.LENGTH_SHORT。).show();
            }
        };
    }    私人无效alertdialog(INT它)
    {
        开关(所)
        {
        情况下0:
            新AlertDialog.Builder(本)
            .setTitle(警报)
            .setMessage(连接错误!!!+\\ n \\ n+ faHelper.rstring)
            .setPositiveButton(OK,新OnClickListener()
            {
                公共无效的onClick(DialogInterface为arg0,ARG1 INT)
                {
                    Display.this.finish();
                }
            })
            。显示();
            打破;
        情况1:
            新AlertDialog.Builder(本)
            .setTitle(警报)
            .setMessage(错误!+\\ n \\ n+preSS确定,再试一次!)
            .setPositiveButton(OK,新OnClickListener()
            {
                公共无效的onClick(DialogInterface为arg0,ARG1 INT)
                {
                    加载();
                }
            })
            .setPositiveButton(取消,新OnClickListener()
            {
                公共无效的onClick(DialogInterface为arg0,ARG1 INT)
                {
                    成功= 1;
                    Display.this.finish();
                }
            })
            。显示();
            打破;
        }
    }    公共地图<字符串,> CREATEOBJECT(标题字符串,字符串图片)
    {
        地图<字符串,字符串>项目=新的HashMap<字符串,字符串>();
        item.put(题,题);
        item.put(图像,图像);
        归还物品;
    }    公共无效刷新(查看视图)
    {
        加载();
    }    私人无效负载()
    {
        新PBAR()执行(URL);
    }    私人字符串的getURL(捆绑savedInstanceState)
    {
        字符串URL =(savedInstanceState == NULL)?空:(字符串)savedInstanceState.getSerializable(URL);        如果(URL == NULL)
        {
            捆绑额外= getIntent()getExtras()。
            URL =临时演员!= NULL? extras.getString(URL):空;
        }        返回URL;
    }    @覆盖
    保护无效的onSaveInstanceState(捆绑outState)
    {
        super.onSaveInstanceState(outState);
        outState.putSerializable(URL,网址);
    }    @覆盖
    保护无效的onPause()
    {
        super.onPause();
    }    @覆盖
    保护无效onResume()
    {
        super.onResume();
    }
}

这行code是出于某种原因,创建一个空指针异常,我不知道为什么。

list.setOnItemClickListener(itemclicklistener());

当我只是简单地创建带有code列表视图,它似乎工作得很好。但是,当IM设置实际上它是一个的LinearLayout内的列表视图,一切都只是点击监听罚款。任何建议,将有助于充分。

在xml的如下:

displaylist.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>
        <按钮
            安卓的onClick =刷新
            机器人:文字=刷新
            机器人:ID =@ + ID /刷新
            机器人:layout_width =match_parent
            机器人:layout_height =32dp>
        < /按钮>
    < / LinearLayout中>
    < ListView控件
        机器人:ID =@ + ID / DLIST
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>
        < /&的ListView GT;
< / LinearLayout中>

header.xml

 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / demoheader_title
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:paddingTop =2DIP
    机器人:paddingBottom会=2DIP
    机器人:paddingLeft =5dip
    机器人:背景=#FFFFFF
    风格=机器人:ATTR / listSeparatorTextViewStyle
    />

row.xml

 <的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向
    >    < ImageView的
        机器人:ID =@ + ID /图标
        机器人:layout_width =45dp
        机器人:layout_height =45dp
        机器人:宽=50dp
        机器人:身高=50dp
        机器人:scaleType =fitStart
        机器人:paddingRight =5DP
        />    <的TextView
        机器人:ID =@ + ID /文本2
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:单线=假
        机器人:textAppearance =机器人:ATTR / textAppearanceSmall
        />
< / LinearLayout中>


解决方案

如果你不使用机器人ID为的ListView 您无法通过 ListActivity 你必须将其与扩展活动

您应该使用机器人ID如果通过ListActivity扩展类

 机器人:ID =@机器人:ID /名单

但是,如果您使用的是机器人:ID =@ + ID / DLIST,那么你必须用扩展Activity类

另外你展示使用的setContentView(R.layout.displaylist); 获取的ID之前

 列表=(ListView控件)findViewById(R.id.dlist);
        list.setOnItemClickListener(itemclicklistener());

public class Display extends ListActivity 
{
    private ForumAdapter faHelper;
    private ProgressDialog dialog;  
    private String Url;
    private int success = 0;

    private SeparatedListAdapter sAdapter;
    private ListView list;

    private List<Map<String,?>> cat_list;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setTitle("Board Index");

        list = (ListView) findViewById(R.id.dlist);
        list.setOnItemClickListener(itemclicklistener());

        setContentView(R.layout.displaylist);

        faHelper = new ForumAdapter(this);

        Url = geturl(savedInstanceState);

        if (faHelper.isGood(Url))
        {
            Load();
        }
        else
        {
            alertdialog(0);
        }
    }

    private class pbar extends AsyncTask<String, Void, Integer> 
    {
        private final String message = "Loading. Please wait...";

        @Override
        protected void onPreExecute() 
        {
            dialog = ProgressDialog.show(Display.this, "", message, true);
        }

        @Override
        protected void onPostExecute(Integer result) 
        {
            success = result;
            dialog.dismiss();
            displaystuff();
        }

        protected Integer doInBackground(String... url) 
        {
            return faHelper.SourceDownload(url[0]);
        }
    }

    private void displaystuff()
    {
        if (success == 1)
        {
            sAdapter = new SeparatedListAdapter(this, R.layout.demoheader);

            for (Object row : faHelper.BoardIndex)
            {
                String[][] theRow = (String[][])row;
                String ctitle = "";
                cat_list = new LinkedList<Map<String,?>>();

                for (int i = 0; i < theRow.length; i++)
                {
                    if (i == 0) ctitle = theRow[i][0];

                    /* + "\n" + theRow[i][2]*/
                    cat_list.add(createObject(theRow[i][1], theRow[i][3]));
                }

                ExAdapter exone = new ExAdapter(this, R.layout.demorow, cat_list);
                sAdapter.addSection(ctitle, exone);
            }
            list.setAdapter(sAdapter);
        }
        else
        {
            alertdialog(1);
        }
    }

    private OnItemClickListener itemclicklistener()
    {
        return new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
                @SuppressWarnings("unchecked") // Object to Map.
                Map<String,?> one = (Map<String,?>) parent.getAdapter().getItem(position);
                Toast.makeText(getApplicationContext(), one.get("title").toString(), Toast.LENGTH_SHORT).show();
            }
        };
    }

    private void alertdialog(int which)
    {
        switch (which)
        {
        case 0:
            new AlertDialog.Builder(this)
            .setTitle("Alert")
            .setMessage("Connection Error!!!" + "\n\n" + faHelper.rstring)
            .setPositiveButton("OK", new OnClickListener() 
            {
                public void onClick(DialogInterface arg0, int arg1) 
                {
                    Display.this.finish();
                }
            })
            .show();
            break;
        case 1:
            new AlertDialog.Builder(this)
            .setTitle("Alert")
            .setMessage("Error!!" + "\n\n" + "Press OK to try again!")
            .setPositiveButton("OK", new OnClickListener() 
            {
                public void onClick(DialogInterface arg0, int arg1) 
                {
                    Load();
                }
            })
            .setPositiveButton("Cancel", new OnClickListener() 
            {
                public void onClick(DialogInterface arg0, int arg1) 
                {
                    success = 1;
                    Display.this.finish();
                }
            })
            .show();
            break;
        }
    }

    public Map<String,?> createObject(String title, String image) 
    {
        Map<String,String> item = new HashMap<String,String>();  
        item.put("title", title);
        item.put("image", image);
        return item;
    }

    public void refresh(View view)
    {
        Load();
    }

    private void Load()
    {
        new pbar().execute(Url);
    }

    private String geturl(Bundle savedInstanceState)
    {
        String url = (savedInstanceState == null) ? null : (String) savedInstanceState.getSerializable("URL");

        if (url == null) 
        {
            Bundle extras = getIntent().getExtras();
            url = extras != null ? extras.getString("URL") : null;
        }

        return url;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);
        outState.putSerializable("URL", Url);
    }

    @Override
    protected void onPause() 
    {
        super.onPause();
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
    }
}

this line of code is for some reason creating a null pointer exception, and i have no idea why.

list.setOnItemClickListener(itemclicklistener());

when i just simply create a listview with code, it seems to work just fine. but when im actually setting it to be a listview inside a linearlayout, everything is fine except for the click listener. any advice would be help full.

the xml's are as follows:

displaylist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:orientation="vertical">    

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button 
            android:onClick="refresh" 
            android:text="Refresh" 
            android:id="@+id/refresh" 
            android:layout_width="match_parent"
            android:layout_height="32dp">
        </Button>
    </LinearLayout>
    <ListView 
        android:id="@+id/dlist" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">
        </ListView>
</LinearLayout>

header.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/demoheader_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:paddingLeft="5dip"
    android:background="#FFFFFF"
    style="?android:attr/listSeparatorTextViewStyle"
    />

row.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:width="50dp"
        android:height="50dp"
        android:scaleType="fitStart"
        android:paddingRight="5dp"
        />

    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:textAppearance="?android:attr/textAppearanceSmall"  
        />  
</LinearLayout>

解决方案

If you are not using androids id for ListView you cannot extends the class by ListActivity you have to extend it with Activity.

You should use Androids id if you extend Class by ListActivity

android:id="@android:id/list

But if you are using android:id="@+id/dlist" then you have to extend class with Activity

Also you show use setContentView(R.layout.displaylist); before fetching the id

list = (ListView) findViewById(R.id.dlist);
        list.setOnItemClickListener(itemclicklistener());

这篇关于我能做些什么这个空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆