运行

时间:2018-04-04 11:37:02

标签: java android mysql android-layout android-volley

-------第一个代码-----------

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
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.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
TextView textViewId, textViewUsername, textViewEmail, textViewGender;
@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);

    //add this line to display menu1 when the activity is loaded
    //displaySelectedScreen(R.id.nav_menu1);
    if (!SharedPrefManager.getInstance(this).isLoggedIn()) {
        finish();
        startActivity(new Intent(this, Login.class));
    }


    textViewId = (TextView) findViewById(R.id.textViewId);
    textViewUsername = (TextView) findViewById(R.id.textViewUsername);
    textViewEmail = (TextView) findViewById(R.id.textViewEmail);
    textViewGender = (TextView) findViewById(R.id.textViewGender);


    //getting the current user
    User user = SharedPrefManager.getInstance(this).getUser();

    //setting the values to the textviews
    textViewId.setText(String.valueOf(user.getId()));
    textViewUsername.setText(user.getUsername());
    textViewEmail.setText(user.getEmail());
    textViewGender.setText(user.getGender());

    //when the user presses logout button
    //calling the logout method
    findViewById(R.id.buttonLogout).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
            SharedPrefManager.getInstance(getApplicationContext()).logout();
        }
    });

}
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@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_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void displaySelectedScreen(int itemId) {

    //creating fragment object
    Fragment fragment = null;

    //initializing the fragment object which is selected
    switch (itemId) {
        case R.id.staff_link:
            fragment = new Staff_layout();
            break;
        case R.id.student_link:
            fragment = new Student_layout();
            break;
        case R.id.vehicle_link:
            fragment = new Vehicle_layout();
            break;
    }

    //replacing the fragment
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    //calling the method displayselectedscreen and passing the id of selected menu
    displaySelectedScreen(item.getItemId());
    //make this method blank
    return true;
}


}

当我只放置正在运行的第二个代码并在布局中显示数据但在添加第一个代码后它正在关闭

------第二个代码---------

public class MainActivity extends AppCompatActivity


{
TextView textViewId, textViewUsername, textViewEmail, textViewGender;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (!SharedPrefManager.getInstance(this).isLoggedIn()) {
        finish();
        startActivity(new Intent(this, Login.class));
    }


    textViewId = (TextView) findViewById(R.id.textViewId);
    textViewUsername = (TextView) findViewById(R.id.textViewUsername);
    textViewEmail = (TextView) findViewById(R.id.textViewEmail);
    textViewGender = (TextView) findViewById(R.id.textViewGender);


    //getting the current user
    User user = SharedPrefManager.getInstance(this).getUser();

    //setting the values to the textviews
    textViewId.setText(String.valueOf(user.getId()));
    textViewUsername.setText(user.getUsername());
    textViewEmail.setText(user.getEmail());
    textViewGender.setText(user.getGender());

    //when the user presses logout button
    //calling the logout method
    findViewById(R.id.buttonLogout).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
            SharedPrefManager.getInstance(getApplicationContext()).logout();
        }
    });


}


}

当执行第一个代码时,它根本不运行,但是当第二个代码执行时,它正在执行如何将navigationslider添加到第二个代码或如何纠正第一个代码

日志 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.jishwanth.two / com.example.jishwanth.two.MainActivity}:java.lang.IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏。不要在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏。                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2681)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2742)                       在android.app.ActivityThread.-wrap12(ActivityThread.java)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1489)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:154)                       在android.app.ActivityThread.main(ActivityThread.java:6178)                       at java.lang.reflect.Method.invoke(Native Method)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:891)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)                    引起:java.lang.IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏。不要在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏。                       在android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:201)                       在android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)                       在com.example.jishwanth.two.MainActivity.onCreate(MainActivity.java:28)                       在android.app.Activity.performCreate(Activity.java:6682)                       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2634)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2742)                       在android.app.ActivityThread.-wrap12(ActivityThread.java)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1489)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:154)                       在android.app.ActivityThread.main(ActivityThread.java:6178)                       at java.lang.reflect.Method.invoke(Native Method)

1 个答案:

答案 0 :(得分:0)

yourlog cat表示Activity已经有一个由窗口装饰提供的操作栏。 因此,在styles.xml的values文件夹中,用

替换第一行
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

原因:由于您使用自己的工具栏,因此必须删除默认工具栏。