まず、これは2つの部分からなる質問です。 私はアプリでナビゲーションバーを使い始めていますが、このバーを使ってビューを変更すると問題が起こります。コンテンツビューのクリア
最初の画像は、すべてのフィールド/ボタンが難治あり、私のアプリのスタートアップを示しています。 2番目の画像は、ナビゲーションバーから項目を選択するとどうなるかを示しています。呼び出されているビューは、「SCAN」と呼ばれるボタンのみで構成されていますが、この新しいビューと以前のビューが結合されているだけです。ここで
が同じMainActivityの最初のビュー/ MainActivity
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
から関連するコードで、ここではナビゲーションバーを扱うコードがあります。 Nav_Frameは、content_main.xmlで宣言されたFrameLayoutです。
public boolean onNavigationItemSelected(MenuItem item)
{
// Handle navigation view item clicks here.
int id = item.getItemId();
android.app.FragmentManager FragManager = getFragmentManager();
if (id == R.id.nav_Menu) {
FragManager.beginTransaction().replace(R.id.Nav_Frame,new MainScreen()).commit();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
これは私が私の問題は、このクラスに座っていると思いナビゲーションバー
public class MainScreen extends Fragment
{
View MyView;
Button Scan;
RelativeLayout RelLay;
MainActivity MA= new MainActivity();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
RelLay=(RelativeLayout)MA.findViewById(R.id.RelativeLayout);
RelLay.removeAllViews();
MyView = inflater.inflate(R.layout.home_screen,container,false);
Scan=(Button)MyView.findViewById(R.id.btnScan);
return MyView;
}
}
によって呼び出されるMainScreen()クラスです。昨日のある種のグーグルでは、.removeAllViews()メソッドが必要なように思えました。しかし、どのRelativeLayoutが、MainActivity()、またはMainScreen()であるかはわかりません。
もう1つの問題は、RelLayラインが、アプリケーションをクラッシュさせて、NullPointerExceptionを出してしまうことです。
ありがとうございます。
うーん、私はこれをどのように行うだろう動作しますか? – Demonic217