【问题标题】:getting the values of seekbars and passing values as intents to new activity获取搜索栏的值并将值作为意图传递给新活动
【发布时间】:2013-10-21 14:25:48
【问题描述】:

我在 AndroidTabRestaurantDescFilterListView.java 中得到 NULL 值

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);
            }
        });


    }

}

日志

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) 

我的分析::

  • 我能够从搜索栏获取值 PRICEbar,DISTANCEbar,RATINGbar
  • 我作为意图从filter.java 传递到 AndroidTabRestaurantDescFilterListView.java .... 这三个都是从搜索栏获得的整数
  • 但我得到 NULL 值 AndroidTabRestaurantDescFilterListView.java 上课时收到 它们作为下一个活动的意图

调试结果::**for **AndroidTabRestaurantDescFilterListView

this    AndroidTabRestaurantDescFilterListView  (id=830102681688)   
tabHost TabHost  (id=830102691544)  
savedInstanceState  null    
REST1   "Chinese" (id=830102732832) 
PriceBar    null    
DistanceBar null    
  • 为什么我在这里得到 NULL ?

  • 如何调试?

【问题讨论】:

  • 首先,您的代码真的很难阅读,请查看oracle.com/technetwork/java/javase/documentation/… e.g.变量应该以小写字母开头,看起来您正在将对象分配给类...第二,如果我没看错,您“要求”“StringExtras”而不是“整数”?但老实说,我真的不明白你的代码......对不起
  • PRICEbar.getProgress() 返回 int 值对吗?
  • @ Raghunandan ......实际上......我在搜索栏上看到整数值读数......据此判断......是的,它返回 int......我在某处犯了错误在这里 int & string !
  • @smriti3 那么为什么在尝试检索值时使用getStringExtra 并且下次只发布发送和检索部分。只发布相关部分
  • @smriti3 是的,你应该这样做。

标签: java android android-intent


【解决方案1】:

seekbar.getProgress() 返回一个 int 值。

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()

所以在检索值时使用

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.

【讨论】:

    【解决方案2】:

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

    应该是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多