0

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

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

  • 空指针异常是如何发生的

  • 如何解决这个问题

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


    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
        Done=(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);



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

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

        Done.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent DoneIntent=new Intent(Filters.this,AndroidTabRestaurantDescFilterListView.class);

                DoneIntent.putExtra("REST1",selectedType);
                DoneIntent.putExtra("PriceBar", PRICEbar.getProgress());
                DoneIntent.putExtra("DistanceBar", DISTANCEbar.getProgress());
                DoneIntent.putExtra("RatingBar", RATINGbar.getProgress());

                startActivity(DoneIntent);
            }
        });


    }
    @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

    }



}

AndroidTabRestaurantDescListView.java

public class AndroidTabRestaurantDescListView 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();

        // Inbox Tab
        TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
        Intent inboxIntent = new Intent(this, MainActivity.class);
        inboxSpec.setIndicator(INBOX_SPEC);
        // Tab Content
        inboxSpec.setContent(inboxIntent);

        // Outbox Tab
        TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);
        Intent PriceIntent = new Intent(this, PriceDescriptionActivity.class);
        PriceSpec .setIndicator(OUTBOX_SPEC);
        PriceSpec.setContent(PriceIntent);

        // Profile Tab
        TabSpec DistanceSpec = tabHost.newTabSpec(PROFILE_SPEC);
        Intent DistanceIntent = new Intent(this, DistanceDiscriptionActivity.class);
        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(AndroidTabRestaurantDescListView.this,AndroidTabRestaurantDescImageListView.class);
                startActivity(PhotoIntent);

            }
        });


        Filter.setOnClickListener(new OnClickListener() {

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


        Search.setOnClickListener(new OnClickListener() {

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


    }

}

日志

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

编辑

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

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

4

3 回答 3

0

你还没有初始化PRICEbar, DISTANCEbar, RATINGbar... 并且你试图访问它们

尝试添加这些行

 PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID);  
 DISTANCEbar   = (SeekBar)findViewById(R.id.DISTANCEbarID); 
 RATINGbar = (SeekBar)findViewById(R.id.RATINGbarID); 

 Done=(Button) findViewById(R.id.SEARCH_BUTTON_ID);
于 2013-10-21T08:44:07.253 回答
0

你没有初始化PRICEbar. 这样做:

PRICEbar =(SeekBar) findViewById(R.id.pricebar); 

同时初始化其他 Seekbar。

于 2013-10-21T08:44:18.683 回答
0

尝试这个..

back=(Button) findViewById(R.id.TopNavigationBarFilterBackButton);

back=(Button) findViewById(R.id.TopNavigationBarRestaurantSearchActivityBackButton);

有两个后 btn 初始化尝试给出确切的一个..

于 2013-10-21T08:53:49.197 回答