2016-07-03 13 views
0

我在android中編程。當我使用(LoginActivity)下一步是加載在MainActivity記錄sucessfull,這MainActivity它是一個導航抽屜活性,但我得到這個錯誤:登錄後無法上傳MainActivity succesfull(LoginActivity)Android Studio

致命異常:主 工藝:com.example.administrador.vicon,PID :32149 java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.administrador.vicon/com.example.administrador.vicon.MainActivity}:java.lang.IllegalStateException:該活動已經有一個由窗口裝飾。請勿在您的主題中請求Window.FEATURE_SUPPORT_ACTION_BAR並將windowActionBar設置爲false以代替使用工具欄。

注意:該記錄完美地工作,只有問題是沒有上傳主要活動。

我有以下文件:

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.administrador.vicon"> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".LoginActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".RegisterActivity" /> 
     <activity android:name=".MainActivity"></activity> 
    </application> 

</manifest> 

LoginActivity.java

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.administrador.vicon"> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".LoginActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".RegisterActivity" /> 
     <activity android:name=".MainActivity"></activity> 
    </application> 

</manifest> 

LoginRequest.java

import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.toolbox.StringRequest; 

import java.util.HashMap; 
import java.util.Map; 

/** 
* Created by PcD on 03/07/2016. 
*/ 
public class LoginRequest extends StringRequest { 
    private static final String REGISTER_REQUEST_URL = "http://bb.com/Login.php"; 
    private Map<String, String> params; 

    public LoginRequest(String username, String password, Response.Listener<String> listener) 
    { 
     super(Request.Method.POST, REGISTER_REQUEST_URL, listener, null); 
     params = new HashMap<>(); 

     params.put("username", username); 
     params.put("password",password); 

    } 

    @Override 
    public Map<String, String> getParams() 
    { 
     return params; 
    } 
} 

MainActivity.java

package com.example.administrador.vicon; 

import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.Fragment; 
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; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener, jfragment_informacion_tarifas.OnFragmentInteractionListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Hola Soy Ivan", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

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

    @Override 
    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); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     Fragment fragment=null; 
     boolean FragmentTransaction = false; 

     if (id == R.id.nav_informacion_de_tarifas) { 
      fragment = new jfragment_informacion_tarifas(); 
      FragmentTransaction= true; 

     } else if (id == R.id.nav_informacion_personal) { 

     } else if (id == R.id.nav_consumo_tiempo_real) { 



     } else if (id == R.id.nav_historial_consumo) { 

     } else if (id == R.id.nav_graficas) { 

     } 

     if (FragmentTransaction) 
     { 
      getSupportFragmentManager().beginTransaction().replace(R.id.content_main, fragment).commit(); 
      item.setChecked(true); 
      getSupportActionBar().setTitle(item.getTitle()); 
     } 

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

    @Override 
    public void onFragmentInteraction(Uri uri) { 

    } 
} 

activity_login.xml

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
    > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:id="@+id/textView" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/etUsername" 
    android:layout_below="@+id/textView" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentEnd="true" 
    android:hint="Username" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPassword" 
    android:ems="10" 
    android:id="@+id/etPassword" 
    android:layout_below="@+id/etUsername" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentEnd="true" 
    android:hint="Password" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Login" 
    android:id="@+id/bLogin" 
    android:layout_below="@+id/etPassword" 
    android:layout_alignEnd="@+id/etPassword" 
    android:layout_toEndOf="@+id/textView" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Register Here" 
    android:id="@+id/tvRegisterHere" 
    android:layout_below="@+id/bLogin" 
    android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.administrador.vicon.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:id="@+id/content_main" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.administrador.vicon.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" /> 
</RelativeLayout> 

nav_header_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    android:weightSum="1"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="103dp" 
     android:layout_height="62dp" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:src="@drawable/icono_app" 
     android:contentDescription="sdf" 
     android:layout_gravity="center_horizontal" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="Android Studio" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]" /> 

</LinearLayout> 

styles.xml

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 

    </style> 

    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

</resources> 

styles.xml(V21)

<resources> 

    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">true</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/transparent</item> 


    </style> 
</resources> 

感謝,在給定的時間。

+0

後烏爾stayles XML – Sush

+0

在你AppTheme風格的使用, Theme.AppCompat.Light.NoActionBar 而不是 Theme.AppCompat.Light.DarkActionBar 您正在設置一個包含操作欄和t如果您將工具欄設置爲操作欄。這就是爲什麼你得到這個錯誤。 使用沒有操作欄的主題,它會解決問題。 –

+0

Thanksssss Waqar Younis你的推薦解決了我的問題,謝謝所有人的幫助。 –

回答

-1

使用下面的項目或複製此的都在你的styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 

    <item name="windowActionBar">true</item> 


</style> 

或艙單

<activity android:name=".MainActivity" android:theme="@style/AppTheme.NoActionBar" ></activity> 
+0

感謝您的幫助,我爲您提出了更改建議,但我得到了同樣的錯誤。 –

+0

查看我的最新評論 – Sush

+0

這不行。你的'AppTheme'應該有AppCompat的'NoActionBar'風格作爲父類。 –