我在fragment.xml中定义了一个布局,在活动中调用.... 当我按下父对象操作栏中的一个按钮时,我想让它显示在onOptionsItemSelected()中。当我这样做时,它会产生一个空对象引用错误....
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
@Override
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);
}
mainmenu.xml
<item
android:id="@+id/action_job_confirm"
android:title="job confirm"
app:showAsAction="never"/>
答案 0 :(得分:0)
为你的片段做这样的事情:
public class testFragment extends Fragment implements Handler.Callback{
public final Handler handler = new Handler(testFragment.this);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View root = inflater.inflate(R.layout.fragment, container, false);
}
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == 1) {
//do your job here
}
return false;
}
}
从您的活动中调用它:
testFragment frag = (testFragment) getFragmentManager()
.findFragmentByTag("Fragment_1");
Message ms = Message.obtain(frag.handler,1);
frag.handler.sendMessage(ms);
答案 1 :(得分:0)
在您的代码上,初始化
RelativeLayout layoutCurrentJob=(RelativeLayout) findViewById(R.id.relative_layout_current_job);
错了。因为你想要隐藏/显示的RelativeLayout是你的片段的一部分。你不能用它来初始化RelativeLayout。
它应该如下面片段中的onCreateView
所示@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View root = inflater.inflate(R.layout.fragment, container, false);
RelativeLayout layoutCurrentJob=(RelativeLayout) findViewById(R.id.relative_layout_current_job);
}`
您可以通过以下方法
访问Fragment的小部件RelativeLayout layoutCurrentJob = (RelativeLayout )fragmentobj.getView().findViewById(R.id.img_one));
layoutCurrentJob.setVisibility(View.VISIBLE);