片段布局调试问题

时间:2017-05-24 21:44:50

标签: java android xml android-fragments

这是我在主要活动上的java代码:

csv.reader()

这是我在Cajon.java中的代码java:

from azure.mgmt.resource import SubscriptionClient
...
subscriptionClient = SubscriptionClient(credentials)
for subscription in subscriptionClient.subscriptions.list():
    print subscription
...

}

这些是我的.xml文件: MainActivity:

    package com.jcc.javier.navegacioncajon;


    import android.app.Activity;
    import android.support.v4.app.Fragment;
    import android.app.FragmentTransaction;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.ViewGroup;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity implements 
    Cajon.NavegationDrawerCallbacks {

private Cajon mNavigationDrawerFragment;
private CharSequence mTitle;

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

    mNavigationDrawerFragment = (Cajon) getSupportFragmentManager().findFragmentById(R.id.navegacionCajon);
    mTitle = getTitle();
    mNavigationDrawerFragment.setUp(R.id.navegacionCajon, (DrawerLayout) findViewById(R.id.cajon_layout));

}

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

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER="numero_Seccion";


    public PlaceholderFragment() {
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.seccion);
        textView.setText("Este es el contenido de la seccion " + Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }

    public static PlaceholderFragment newInstance (int numeroseccion){
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, numeroseccion);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity)activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

@Override
public void onNavigationDrawerItemSelected(int position) {

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position+1)).commit();

}

public void onSectionAttached (int number){
    switch (number){
        case 1:
            mTitle = getString(R.string.seccion1);
            break;
        case 2:
            mTitle = getString(R.string.seccion2);
            break;
        case 3:
            mTitle = getString(R.string.seccion3);
            break;
    }
}

public void restoreActionBar(){
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);

}
    }

全局:

  package com.jcc.javier.navegacioncajon;

  import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.res.Configuration;
  import android.app.FragmentTransaction;
  import android.os.Bundle;
  import android.preference.Preference;
  import android.preference.PreferenceManager;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.support.v4.view.GravityCompat;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.ActionBar;
  import android.support.v4.app.ActionBarDrawerToggle;
  import android.support.v7.app.ActionBarActivity;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.Toast;

  import java.util.zip.Inflater;

  /**
  * Created by JAVIER on 05/05/2017.
  */

  public class Cajon extends Fragment {

/**Interface para apuntador **/
public static interface NavegationDrawerCallbacks {

    void onNavigationDrawerItemSelected(int position);

}

private static final String STATE_SELECTED_POSITION="selected_navigation_drawer_position";

private static final String PREF_USER_LEARNED_DRAWER="navigation_drawer_learned";

private NavegationDrawerCallbacks mCallbacks;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mfragmentContainerView;

private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;

/**Implementación de clase Cajon **/
public Cajon() {
}

/**Metodo onCreate **/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

    /**Validar si existe un valor en la instancia salvada **/
    if (savedInstanceState != null){
        mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
        mFromSavedInstanceState = true;
    }
    //mDrawerLayout=null;
    selectedItem(mCurrentSelectedPosition);

}

/**Metodo onActivityCreated **/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mDrawerListView = (ListView) inflater.inflate(R.layout.cajon, container,false);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            selectedItem(position);
        }
    });
    mDrawerListView.setAdapter(new ArrayAdapter<String>(
            //getActivity(),
            getSupportActionBar().getThemedContext(),
            android.R.layout.simple_list_item_1,
            android.R.id.text1,
            new String[]{
                    getString(R.string.seccion1),
                    getString(R.string.seccion2),
                    getString(R.string.seccion3),
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;
}

public boolean isDrawerOpen(){
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mfragmentContainerView);

}

public void setUp(int fragmentId, DrawerLayout drawerLayout){
    mfragmentContainerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;
    /**Poner sombra **/
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(
            getActivity(),
            mDrawerLayout,
            R.drawable.ic_drawer,
            R.string.abrir_cajon_navegacion,
            R.string.cerrar_cajon_navegacion
    ) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if(!isAdded()){
                return;
            }
            if(!mUserLearnedDrawer){
                mUserLearnedDrawer = true;
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
                sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).commit();
            }
            getActivity().supportInvalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if(!isAdded()){
                return;
            }
            getActivity().supportInvalidateOptionsMenu();
        }
    };
    if(!mUserLearnedDrawer && !mFromSavedInstanceState){
        mDrawerLayout.openDrawer(mfragmentContainerView);
    }
    mDrawerLayout.post(new Runnable() {

        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}

private ActionBar getSupportActionBar() {
    return ((AppCompatActivity)getActivity()).getSupportActionBar();

}

/**Metodo selectedItem **/
private void selectedItem(int position) {
    mCurrentSelectedPosition = position;
    if(mDrawerListView != null){
        mDrawerListView.setItemChecked(position, true);
    }
    if(mDrawerLayout != null){
        mDrawerLayout.closeDrawer(mfragmentContainerView);
    }
    if(mCallbacks != null){
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
}

@Override
public String toString() {
    return super.toString();
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mCallbacks = (NavegationDrawerCallbacks) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException("Activity must implement Navigation NavigationDrawerCallbacks. ");
    }
}
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}

@Override
public void onDetach() {
    super.onDetach();
    mCallbacks = null;
}

@Override
public void setArguments(Bundle args) {
    super.setArguments(args);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if (mDrawerLayout != null && isDrawerOpen()) {
        inflater.inflate(R.menu.global, menu);
        showGlobalContextActionBar();
    }
    super.onCreateOptionsMenu(menu, inflater);
}

private void showGlobalContextActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setTitle(R.string.app_name);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mDrawerToggle.onOptionsItemSelected(item)){
        return true;
    }
    if(item.getItemId()==R.id.action_example){
        Toast.makeText(getActivity(), "Navegacion Cajon", Toast.LENGTH_LONG).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

主:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cajon_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jcc.javier.navegacioncajon.MainActivity"
tools:ignore="MergeRootFrame">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

    <fragment
        android:id="@+id/navegacionCajon"
        android:name = "com.jcc.javier.navegacioncajon.Cajon"
        android:layout_width="@dimen/navegacionCajon"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/cajon" />


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

片段主要:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
    </menu>

Cajon的:

   <menu 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"
   tools:context="com.jcc.javier.navegacioncajon.MainActivity">
   <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
   <item
    android:id="@+id/action_example"
    android:title="@string/action_example"
    app:showAsAction="ifRoom|withText"></item>

   </menu>

我的logcat显示以下消息:

    <?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:background="#81F79F"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jcc.javier.navegacioncajon.MainActivity$PlaceholderFragment">

<TextView
    android:id="@+id/seccion"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:textSize="20sp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Contenido"
    android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

我需要解决这个错误,因为我无法运行我的应用程序。

2 个答案:

答案 0 :(得分:0)

getSupportActionBar()返回null。确保您的活动使用的主题提供了ActionBar(AppCompat主题)或在setSupportActionBar(toolbar)中使用MainActivity.onCreate()

答案 1 :(得分:0)

改变这个:

SELECT a.id, a.firstName, a.lastName, GROUP_CONCAT(i.fileName SEPARATOR ',') AS images
FROM accounts a
LEFT JOIN images i ON (a.id = i.accountID)
GROUP BY a.id

要:

import UIKit
import MapKit

class AddressVC: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!
    var searchCompleter = MKLocalSearchCompleter()
    var searchResults = [MKLocalSearchCompletion]()




    @IBOutlet weak var searchResultsTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.delegate = self
    // Do any additional setup after loading the view, typically from a nib.

        searchResultsTableView.isHidden = true

        searchCompleter.delegate = self
    }




extension AddressVC: UISearchBarDelegate {

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    searchCompleter.queryFragment = searchText
}

func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
    self.searchBar.endEditing(true)
    searchBar.resignFirstResponder()
    return true
}

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    searchResultsTableView.isHidden = false
    return true

}

func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
    self.searchBar.endEditing(true)
    searchBar.resignFirstResponder()
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchResultsTableView.isHidden = true
    self.searchBar.endEditing(true)
    searchBar.resignFirstResponder()

}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}


}