0

我正在尝试通过单击按钮启动活动

日志

10-21 15:26:58.747: D/AndroidRuntime(693): Shutting down VM
10-21 15:26:58.747: W/dalvikvm(693): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-21 15:26:58.767: E/AndroidRuntime(693): FATAL EXCEPTION: main
10-21 15:26:58.767: E/AndroidRuntime(693): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.jsonparsetutorial/com.project.findmybuffet.Filters}: java.lang.NullPointerException
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.os.Looper.loop(Looper.java:123)
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-21 15:26:58.767: E/AndroidRuntime(693):  at java.lang.reflect.Method.invokeNative(Native Method)
10-21 15:26:58.767: E/AndroidRuntime(693):  at java.lang.reflect.Method.invoke(Method.java:521)
10-21 15:26:58.767: E/AndroidRuntime(693):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-21 15:26:58.767: E/AndroidRuntime(693):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-21 15:26:58.767: E/AndroidRuntime(693):  at dalvik.system.NativeStart.main(Native Method)
10-21 15:26:58.767: E/AndroidRuntime(693): Caused by: java.lang.NullPointerException
10-21 15:26:58.767: E/AndroidRuntime(693):  at com.project.findmybuffet.Filters.onCreate(Filters.java:181)
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-21 15:26:58.767: E/AndroidRuntime(693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-21 15:26:58.767: E/AndroidRuntime(693):  ... 11 more
10-21 15:27:42.949: I/Process(693): Sending signal. PID: 693 SIG: 9

过滤器.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.SEARCH_BUTTON_ID);

        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,AndroidTabRestaurantDescSearchListView.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

    }



}

放置此代码后出现错误

FILTER.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

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

                startActivity(FilterIntent);
            }
        });

该代码用于启动新活动,因此它是必不可少的

我应该把代码放在哪里filter.java

  • 当我单击按钮启动活动时,我得到null pointer exception
  • 因为我没有将任何价值从一项活动传递到另一项活动

  • 如何解决这个问题

4

3 回答 3

1

我在从布局中引用时提到了错误的 ID ......

代替

FILTER=(Button) findViewById(R.id.TopNavigationBarFilterDoneButton);

我用了

FILTER=(Button) findViewById(R.id.SEARCH_BUTTON_ID);

SathishKumar 提示帮助我解决了这个问题

于 2013-10-21T10:21:41.943 回答
0

AndroidTabRestaurantDescSearchListView.java请在清单文件中提及此类。

于 2013-10-21T10:13:32.317 回答
0

我认为您尚未AndroidTabRestaurantDescSearchListView在 Manifset 中声明 Activity。还要检查您的FILTER视图 ID

于 2013-10-21T10:19:28.480 回答