getting the values of seekbars and passing values

2019-12-16 19:46发布

I am getting NULL values in AndroidTabRestaurantDescFilterListView.java

Filters.java

public class Filters extends Activity implements OnSeekBarChangeListener{

    // declare text objects variables
    private SeekBar PRICEbar,DISTANCEbar, RATINGbar; 
    private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;

    Button back;
    Button FILTER;


    private RadioGroup rdg;
    private RadioButton indian;
    private RadioButton thai;
    private RadioButton chinese;


    private String selectedType="";



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.filters);   



        /** Finding all the views in this Activity. */
        PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
        DISTANCEbar = (SeekBar)findViewById(R.id.DISTANCEseekBarID); // make seekbar object
        RATINGbar = (SeekBar)findViewById(R.id.RATINGseekBarID); // make seekbar object
        FILTER=(Button) findViewById(R.id.TopNavigationBarFilterDoneButton);

        rdg = (RadioGroup) findViewById(R.id.radioGroup1);
        indian = (RadioButton) findViewById(R.id.IndianRG_ID);
        thai = (RadioButton) findViewById(R.id.ThaiRG_ID);
        chinese = (RadioButton) findViewById(R.id.ChineseRG_ID);


        back=(Button) findViewById(R.id.TopNavigationBarFilterBackButton);
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                 finish();
            }
        });






        PRICEbar.setOnSeekBarChangeListener(this); 
        PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
                PRICEtextProgress.setText("Price:: Rs "+progress);
                seekBar.setMax(100);

            }
        });







        ////////////////////////////////////////////////////////
        DISTANCEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                DISTANCEtextProgress = (TextView)findViewById(R.id.DISTANCEtextViewProgressID);
                DISTANCEtextProgress.setText("Distance:: "+progress);
                seekBar.setMax(25);
            }
        });


        ////////////////////////////////////////////////////////
        RATINGbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                RATINGtextProgress = (TextView)findViewById(R.id.RATINGtextViewProgressID);
                RATINGtextProgress.setText("Rating:: "+progress);
                seekBar.setMax(5); 

                RatingBar ratingBar = (RatingBar)findViewById(R.id.RATINGfinalvalueratingID);
                ratingBar.setRating(5);
                ratingBar.setFocusable(false);

                RatingBar ratingBar1 = (RatingBar)findViewById(R.id.RATINGinitialvalueratingID);
                ratingBar1.setRating(0);
                ratingBar.setFocusable(false);
            }
        });


        rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.BreakfastRG_ID){
                    selectedType = indian.getText().toString();
                }else if(i==R.id.LunchRG_ID){
                    selectedType = thai.getText().toString();
                }else{
                    selectedType = chinese.getText().toString();
                }
            }
        });

        FILTER.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent FilterIntent=new Intent(Filters.this,AndroidTabRestaurantDescFilterListView.class);
                FilterIntent.putExtra("REST1",selectedType);
                FilterIntent.putExtra("PriceBar", PRICEbar.getProgress());
                FilterIntent.putExtra("DistanceBar", DISTANCEbar.getProgress());
                FilterIntent.putExtra("RatingBar", RATINGbar.getProgress());

                startActivity(FilterIntent);
            }
        });






    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar == PRICEbar)
            PRICEtextProgress.setText("Price:: Rs "+progress);
        else if (seekBar == DISTANCEbar)
            DISTANCEtextProgress.setText("Distance:: "+progress);
        else if (seekBar == RATINGbar)
            RATINGtextProgress.setText("Rating:: "+progress);
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }



}

AndroidTabRestaurantDescFilterListView.java

public class AndroidTabRestaurantDescFilterListView extends TabActivity {

    // TabSpec Names
    private static final String INBOX_SPEC = "Rating";
    private static final String OUTBOX_SPEC = "Price";
    private static final String PROFILE_SPEC = "Distance";

    Button Photos;
    Button Filter;
    Button Search;

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

        Photos=(Button) findViewById(R.id.PhotoButton); 
        Filter=(Button) findViewById(R.id.FilterButton);
        Search=(Button) findViewById(R.id.SearchBottomBarID);

        TabHost tabHost = getTabHost();


        String REST1 = getIntent().getStringExtra("REST1");
        String PriceBar = getIntent().getStringExtra("PriceBar"); 
        String DistanceBar = getIntent().getStringExtra("DistanceBar");
        String RatingBar = getIntent().getStringExtra("RatingBar");


        // Tab
        TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
        Intent inboxIntent = new Intent(this, RatingDescriptionFilterActivity.class);

        inboxIntent.putExtra("REST1", REST1);
        inboxIntent.putExtra("PriceBar", PriceBar);
        inboxIntent.putExtra("DistanceBar", DistanceBar);
        inboxIntent.putExtra("RatingBar", RatingBar);

        inboxSpec.setIndicator(INBOX_SPEC);
        inboxSpec.setContent(inboxIntent);




        // Tab
        TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);
        Intent PriceIntent = new Intent(this, PriceDescriptionFilterActivity.class);

        PriceIntent.putExtra("REST1", REST1);
        PriceIntent.putExtra("PriceBar", PriceBar);
        PriceIntent.putExtra("DistanceBar", DistanceBar);
        PriceIntent.putExtra("RatingBar", RatingBar);

        PriceSpec .setIndicator(OUTBOX_SPEC);
        PriceSpec.setContent(PriceIntent);



        // Tab
        TabSpec DistanceSpec = tabHost.newTabSpec(PROFILE_SPEC);
        Intent DistanceIntent = new Intent(this, DistanceDescriptionFilterActivity.class);

        DistanceIntent.putExtra("REST1", REST1);
        DistanceIntent.putExtra("PriceBar", PriceBar);
        DistanceIntent.putExtra("DistanceBar", DistanceBar);
        DistanceIntent.putExtra("RatingBar", RatingBar);

        DistanceSpec .setIndicator(PROFILE_SPEC); 
        DistanceSpec.setContent(DistanceIntent);



        // Adding all TabSpec to TabHost
        tabHost.addTab(inboxSpec); 
        tabHost.addTab(PriceSpec); 
        tabHost.addTab(DistanceSpec); 



        //Set the current value tab to default first tab
        tabHost.setCurrentTab(0);

        //Setting custom height for the tabs
        final int height = 45;
        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
        tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = height;




        Photos.setOnClickListener(new OnClickListener() {

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

                Intent PhotoIntent=new Intent(AndroidTabRestaurantDescFilterListView.this,AndroidTabRestaurantDescImageListView.class);
                startActivity(PhotoIntent);

            }
        });


        Filter.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent FilterIntent=new Intent(AndroidTabRestaurantDescFilterListView.this,Filters.class);
                startActivity(FilterIntent);
            }
        });


        Search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent FilterIntent=new Intent(AndroidTabRestaurantDescFilterListView.this,SearchPage.class);
                startActivity(FilterIntent);
            }
        });


    }

}

LOG

10-21 19:53:11.376: E/AndroidRuntime(1393): FATAL EXCEPTION: AsyncTask #2
10-21 19:53:11.376: E/AndroidRuntime(1393): java.lang.RuntimeException: An error occured while executing doInBackground()
10-21 19:53:11.376: E/AndroidRuntime(1393):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at java.lang.Thread.run(Thread.java:1096)
10-21 19:53:11.376: E/AndroidRuntime(1393): Caused by: java.lang.NullPointerException
10-21 19:53:11.376: E/AndroidRuntime(1393):     at com.project.findmybuffet.RatingDescriptionFilterActivity$DownloadJSON.doInBackground(RatingDescriptionFilterActivity.java:86)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at com.project.findmybuffet.RatingDescriptionFilterActivity$DownloadJSON.doInBackground(RatingDescriptionFilterActivity.java:1)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-21 19:53:11.376: E/AndroidRuntime(1393):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-21 19:53:11.376: E/AndroidRuntime(1393):     ... 4 more
10-21 19:53:15.026: E/WindowManager(1393): Activity com.project.findmybuffet.AndroidTabRestaurantDescFilterListView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45fb1c10 that was originally added here
10-21 19:53:15.026: E/WindowManager(1393): android.view.WindowLeaked: Activity com.project.findmybuffet.AndroidTabRestaurantDescFilterListView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45fb1c10 that was originally added here
10-21 19:53:15.026: E/WindowManager(1393):  at android.view.ViewRoot.<init>(ViewRoot.java:247)
10-21 19:53:15.026: E/WindowManager(1393):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-21 19:53:15.026: E/WindowManager(1393):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-21 19:53:15.026: E/WindowManager(1393):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-21 19:53:15.026: E/WindowManager(1393):  at android.app.Dialog.show(Dialog.java:241)
10-21 19:53:15.026: E/WindowManager(1393):  at com.project.findmybuffet.RatingDescriptionFilterActivity$DownloadJSON.onPreExecute(RatingDescriptionFilterActivity.java:77)
10-21 19:53:15.026: E/WindowManager(1393):  at android.os.AsyncTask.execute(AsyncTask.java:391)
10-21 19:53:15.026: E/WindowManager(1393):  at com.project.findmybuffet.RatingDescriptionFilterActivity.onCreate(RatingDescriptionFilterActivity.java:60)
10-21 19:53:15.026: E/WindowManager(1393):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-21 19:53:15.026: E/WindowManager(1393):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-21 19:53:15.026: E/WindowManager(1393):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
10-21 19:53:15.026: E/WindowManager(1393):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-21 19:53:15.026: E/WindowManager(1393):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-21 19:53:15.026: E/WindowManager(1393):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
10-21 19:53:15.026: E/WindowManager(1393):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
10-21 19:53:15.026: E/WindowManager(1393):  at android.widget.TabHost.addTab(TabHost.java:213)
10-21 19:53:15.026: E/WindowManager(1393):  at com.project.findmybuffet.AndroidTabRestaurantDescFilterListView.onCreate(AndroidTabRestaurantDescFilterListView.java:87) 

My Analysis::

  • I am able to get the values from the seekbars PRICEbar,DISTANCEbar, RATINGbar
  • I am passing as intents from filter.java to AndroidTabRestaurantDescFilterListView.java .... All three are integers obtained from seekbars
  • But i am getting NULL values in AndroidTabRestaurantDescFilterListView.java class when i recieve them as intents in next activity

Debugging result::**for **AndroidTabRestaurantDescFilterListView

this    AndroidTabRestaurantDescFilterListView  (id=830102681688)   
tabHost TabHost  (id=830102691544)  
savedInstanceState  null    
REST1   "Chinese" (id=830102732832) 
PriceBar    null    
DistanceBar null    
  • Why am i getting NULL here ?

  • How can i debug this ?

2条回答
成全新的幸福
2楼-- · 2019-12-16 20:01

seekbar.getProgress() returns an int value.

public synchronized int getProgress ()

Added in API level 1
Get the progress bar's current level of progress. Return 0 when the progress bar is in indeterminate mode.

Returns
the current progress, between 0 and getMax()

So when retrieving values use

int PriceBar = getIntent().getIntExtra("PriceBar",0); 
int DistanceBar = getIntent().getIntExtra("DistanceBar",0);
int RatingBar = getIntent().getIntExtra("RatingBar",0);

getIntExtra

public int getIntExtra (String name, int defaultValue)

Added in API level 1
Retrieve extended data from the intent.

Parameters
name    The name of the desired item.
defaultValue    the value to be returned if no value of the desired type is stored with the given name.
Returns
the value of an item that previously added with putExtra() or the default value if none was found.
查看更多
地球回转人心会变
3楼-- · 2019-12-16 20:09

String PriceBar = getIntent().getStringExtra("PriceBar");

Should be

int p = getIntent().getIntExtra("PriceBar")

查看更多
登录 后发表回答