私は "スワイプ"プロジェクトを2つの断片の間で実現しました。私は自分の歩行メニューを使ってプロジェクトをしています。私は2つをリンクしようとしますが、私のMainActivityを含んでいるこのアセンブリコードに従うと、私のメニューはフラグメントになる必要があります。多くの機能が言語/文章を変更します。ここでフラグメント+スワイプ+ドローメニュー
私のコードのMainActivityです:
package thyroid.com.thyroidmenu;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.Menu;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends Fragment {
public DrawerLayout drawerLayout;
public Toolbar toolbar;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public GoogleApiClient client;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initNavigationDrawer();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void initNavigationDrawer() {
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
assert navigationView != null;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.home:
Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.connection:
setContentView(R.layout.activity_mainlogin);
Intent myIntent = new Intent(MainActivity.this, login.class);
startActivity(myIntent);
drawerLayout.closeDrawers();
break;
case R.id.settings:
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.trash:
Toast.makeText(getApplicationContext(), "Trash", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.logout:
finish();
}
return true;
}
});
View header = navigationView.getHeaderView(0);
TextView tv_email = (TextView) header.findViewById(R.id.tv_email);
tv_email.setText("[email protected]");
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerClosed(View v) {
super.onDrawerClosed(v);
}
@Override
public void onDrawerOpened(View v) {
super.onDrawerOpened(v);
}
};
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://thyroid.com.thyroidmenu/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://thyroid.com.thyroidmenu/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View MainActivity = inflater.inflate(R.layout.activity_main, container, false);
((TextView)MainActivity.findViewById(R.id.textView)).setText("iOS");
return MainActivity;
}}
そしてここでは、ちょうど悪い執筆しているエラーです:
どのように私は私のsetContentView/getApplicationContextを変更できますか/ findViewById ...などの断片に収まるように?
この小さなモンタージュを実現するには、断片化してプロセスを作らなければならないのですか(写真1参照)?断片は、非常に有用ないずれかの場合には実用的なようで、私なり後で...
感謝;)getActivityによって
こんにちは、あなたの答えを教えてください。 "AppCompactActivity"では、私はまだこのエラーがあります: "エラー:(21、24)エラー:互換性のないタイプ:MainActivityをフラグメントに変換できません"。だから私は "FragmentActivity"を試しましたが、これを取得しました: "エラー:(21、24)エラー:互換性のないタイプ:MainActivityをフラグメントに変換できません" ...任意のアイデア? – zouarv42
@ zouarv42の 'public class MainActivity extends Fragment'を' public class MainActivity extends AppCompatActivity'に置き換えると、あなたが受け取ったエラーログのイメージを投稿できます。変更後の最新のコード。 –
変更あり:) – zouarv42