单击Listview项时出现NullPointerException

时间:2014-07-24 06:12:11

标签: android listview nullpointerexception

  

我在填充ListView时遇到问题。 Logcat节目   NullPointerExcepton。我还在下面发布了代码和logcat。

     

代码是匆忙做的,我只是一个初学者。但我不是   能够解决..那个textview显示getname()返回值..但是它   显示空指针异常..任何建议?

BankFunctionalities.java:

   public class BankFunctionalities extends Activity {

        private ListView ls;
        ArrayAdapter<String> arrayAdapter;
        DetailsVO details;
        private String phoneNumber;
        private String smsSend;
        private String msgNumber;
        private AlertDialog dialog;
        static ArrayList<String> serviceList;


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

            details = (DetailsVO) ApplicationCache.getInstance().getValue("details");

            TextView headerText = (TextView)findViewById(R.id.header_text);
            headerText.setText(details.getName());



            phoneNumber = details.getShortPhoneNumber();

            ImageView callButton = (ImageView)findViewById(R.id.call_icon);

            if(details.getCustomerCareNumber() != null &&
                    !details.getCustomerCareNumber().equalsIgnoreCase("")){
                callButton.setVisibility(View.VISIBLE);
                callButton.setOnClickListener(new OnClickListener() {

                    TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    int simState = telMgr.getSimState();

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                            switch (simState) {
                                case TelephonyManager.SIM_STATE_ABSENT:
                                    // do something
                                    showErrorDialog(BankFunctionalities.this.getString(R.string.no_SIM));
                                    return;
                                case TelephonyManager.SIM_STATE_READY:
                                    // do something
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    intent.setData(Uri.parse("tel:"+details.getCustomerCareNumber()));
                                    startActivity(intent);
                                    //break;
                            }

                    }
                });
            }

            TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            final int simState1 = telMgr.getSimState();

            final LinkedHashMap<String, FunctionVO> map = details.getFunctionalities();

            ls = (ListView)findViewById(R.id.apna_list);

            arrayAdapter = new ArrayAdapter<String>(this, R.layout.apk_list_second_item, R.id.text_item);

            for(String s: map.keySet()){
                arrayAdapter.add(s);
            }

            arrayAdapter.add(details.getsWebsite());

            ls.setAdapter(arrayAdapter);

            ls.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {

                    if(position != map.keySet().size()+1){

                        String sms = map.get(arrayAdapter.getItem(position)).getSmsName();
                        msgNumber = map.get(arrayAdapter.getItem(position)).getMsgNumber();

                        if(sms.contains("$"))
                        {
                            String[] service;
                            serviceList = new ArrayList<String>();
                            service = sms.split("\\$");
                            for(String s : service)
                            {
                                serviceList.add(s);
                            }   

                            Intent serviceIntent = new Intent(BankFunctionalities.this,ApnaService.class);

                            serviceIntent.putExtra("services", serviceList);

                            startActivity(serviceIntent);
                            Log.d("TestTag","serviceList is"+"'"+serviceList);
                        }

                        else if(sms.contains("|")){

                            ApplicationCache.getInstance().setValue("value", sms);

                            Intent detailsIntent = new Intent(BankFunctionalities.this,BankDetailsActivity.class);

                            detailsIntent.putExtra("bankName", details.getName());
                            detailsIntent.putExtra("value", map.get(arrayAdapter.getItem(position)).getSmsName());
                            detailsIntent.putExtra("phoneNumber", details.getShortPhoneNumber());
                            detailsIntent.putExtra("customerCare", details.getCustomerCareNumber());
                            detailsIntent.putExtra("msgNumber", map.get(arrayAdapter.getItem(position)).getMsgNumber());

                            startActivity(detailsIntent);

                        }
                        else
                        {
                            smsSend = sms;

                            switch (simState1) {
                            case TelephonyManager.SIM_STATE_ABSENT:
                                // do something
                                showErrorDialog(BankFunctionalities.this.getString(R.string.no_SIM));
                                return;
                            case TelephonyManager.SIM_STATE_READY:
                                // do something
                                String smsConfirm = BankFunctionalities.this.getResources().getString(R.string.sms_charge).concat(sms);
                                showDialog(smsConfirm);
                                //break;
                        }
                            //showDialog(sms);

                        }
                    }else{
                        //As last item in the list is the website URL
                        //TextView title = (TextView)ls.findViewById(R.id.title);
                        String url = arrayAdapter.getItem(position);
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    }
                }
            });
        }

        private void showDialog(final String message){

            //Created dummy dialog for now
            //Need to implement integration with mBanking server for payment
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    this);

            builder.setMessage(message);

            builder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {


                @Override
                public void onClick(
                        DialogInterface dialog,
                        int which) {
                    String errorMessage = null;
                    try{
                        if(msgNumber != null)
                        {
                            Log.d("Message Number","Number is"+"'"+msgNumber+"'");
                            SmsManager smsManager = SmsManager.getDefault();
                            smsManager.sendTextMessage(msgNumber, null, smsSend, null, null);
                        }
                        else
                        {
                            Log.d("Message Number","Number is"+"'"+phoneNumber+"'");
                            SmsManager smsManager = SmsManager.getDefault();
                            smsManager.sendTextMessage(phoneNumber, null, smsSend, null, null); 
                        }
                    }catch(Exception e){
                        errorMessage = BankFunctionalities.this.getResources().getString(R.string.sms_failed);
                    }
                    dialog.dismiss();
                    //if(errorMessage != null){
                        //showErrorDialog(errorMessage);
                    //}else{
                        //showErrorDialog(BankFunctionalities.this.getString(R.string.sms_success));
                    //}
                }
            });

            dialog = builder.create();
            dialog.show();

        }

        private void showErrorDialog(String message){

            //Created dummy dialog for now
            //Need to implement integration with mBanking server for payment
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    this);

            builder.setMessage(message);
            builder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {

                @Override
                public void onClick(
                        DialogInterface dialog,
                        int which) {
                    dialog.dismiss();
                }
            });

            dialog = builder.create();
            dialog.show();

        }

    }

Apnaservice.java:

public class ApnaService extends Activity
{
    ArrayList<String> list = new ArrayList<String>();
    ListView lv;
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);

        list = BankFunctionalities.serviceList;
        Log.d("TestTag","List is:"+list);
        lv = (ListView) findViewById(R.id.listView_layout);

        adapter = new ArrayAdapter<String>(this,R.layout.testview_info,R.id.text_item);

        for (String s : list)
        {
            adapter.add(s);
        }

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {

                String value = (lv.getItemAtPosition(position).toString());
                Log.d("TestTag","List is inside click:"+list);
                if (value.equalsIgnoreCase("BulletPay"))
                {
                    Intent bulletIntent = new Intent(ApnaService.this,MainScreenTab.class);
                    /*final Intent it = new Intent();
                    it.setClassName("com.bullet.pay", com.bullet.pay.MainScreenTab.class);*/
                    startActivity(bulletIntent);
                }

                /*switch (value) {
                case "BulletPay":
                    Intent bulletIntent = new Intent(ApnaService.this,ListAllMerchants.class);
                    startActivity(bulletIntent);


                }*/



            }

        }); 



    }


}// End ApnaService

这是Logacat:

07-24 01:32:16.500: E/AndroidRuntime(2936): FATAL EXCEPTION: main
07-24 01:32:16.500: E/AndroidRuntime(2936): Process: com.apna.khata, PID: 2936
07-24 01:32:16.500: E/AndroidRuntime(2936): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apna.khata/com.apna.khata.BankFunctionalities}: java.lang.NullPointerException
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.os.Looper.loop(Looper.java:136)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at java.lang.reflect.Method.invokeNative(Native Method)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at java.lang.reflect.Method.invoke(Method.java:515)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at dalvik.system.NativeStart.main(Native Method)
07-24 01:32:16.500: E/AndroidRuntime(2936): Caused by: java.lang.NullPointerException
07-24 01:32:16.500: E/AndroidRuntime(2936):     at com.apna.khata.BankFunctionalities.onCreate(BankFunctionalities.java:49)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.app.Activity.performCreate(Activity.java:5231)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-24 01:32:16.500: E/AndroidRuntime(2936):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-24 01:32:16.500: E/AndroidRuntime(2936):     ... 11 more
07-24 01:32:16.510: W/ActivityManager(1385):   Force finishing activity com.apna.khata/.BankFunctionalities

0 个答案:

没有答案