【问题标题】:page is closing not showing any data while running页面正在关闭,运行时不显示任何数据
【发布时间】:2018-09-13 22:56:02
【问题描述】:

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

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();
        }
    });


}


}

当第一个代码执行时它根本没有运行,但是当第二个代码执行时它正在执行如何将导航滑块添加到第二个代码或如何纠正第一个代码

日志 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.jishwanth.two/com.example.jishwanth.two.MainActivity}:java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 原因:java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 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) 在 java.lang.reflect.Method.invoke(Native Method)

【问题讨论】:

  • 您可以在问题中添加日志数据吗?
  • it is closing 什么正在关闭?

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


【解决方案1】:

yourlog cat 说 Activity 已经有一个由窗口装饰提供的操作栏。 因此,在您的 values 文件夹中的 styles.xml 中,将第一行替换为

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

原因:因为您使用的是自己的工具栏,所以您必须删除默认工具栏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多