I have a layout defined in fragment.xml which is called in activity.... and I want to make it visible when a button in parent activity action bar is pressed which is in onOptionsItemSelected() .when i do this it make a error of null object reference....
fragment.xml
<RelativeLayout android:visibility="invisible" android:id="@+id/relative_layout_current_job" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text_view_current_job" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text_current_job" android:layout_centerHorizontal="true" /> </RelativeLayout> mainActivity.java
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_passenger_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { if (!mNavigationDrawerFragment.isDrawerOpen()) { // Only show items in the action bar relevant to this screen // if the drawer is not showing. Otherwise, let the drawer // decide what to show in the action bar. getMenuInflater().inflate(R.menu.passenger_main, menu); restoreActionBar(); return true; } return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_job_confirm) { Button b = (Button) findViewById(R.id.action_job_confirm); RelativeLayout layoutCurrentJob=(RelativeLayout) findViewById(R.id.relative_layout_current_job); TextView status = (TextView) findViewById(R.id.text_view_current_job_status_in_history); if((b.getText().toString()) == "job confirm") { status.setVisibility(View.INVISIBLE); b.setText("Job Started"); layoutCurrentJob.setVisibility(View.VISIBLE); } else{ status.setVisibility(View.VISIBLE); b.setText("jon confirm"); layoutCurrentJob.setVisibility(View.INVISIBLE); } } return super.onOptionsItemSelected(item); } mainmanu.xml
<item android:id="@+id/action_job_confirm" android:title="job confirm" app:showAsAction="never"/>
No comments:
Post a Comment