我的android项目有问题。今天我打开了一个我正在研究的项目,当我运行它时,所有活动和片段都是空白的,就像我没有设置布局一样,但在我的代码中,一切正常,所有方法都被正确调用
最奇怪的是,当我尝试打开旧项目(已完成且工作正常)时,问题也会发生。我确定这不是代码或IDE的问题(我在eclipse和android studio中执行了代码并且问题仍然存在于booth中)。有人知道它会是什么吗?
答案 0 :(得分:0)
我使用了这些教程(Android Developer):
我的代码如下。
<强> activity_navigation_drawer.xml 强>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffff"/>
</android.support.v4.widget.DrawerLayout>
NavigationDrawer.java(启动器活动)
package recode.mygv.view;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class NavigationDrawer extends Activity implements AdapterView.OnItemClickListener {
private DrawerLayout mDrawerLayout;
private ListView mDrawerLista;
private static ActionBarDrawerToggle mDrawerToggle;
private AdapterDrawerItem adapterCustomDrawer;
private DrawerControl drawerControl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
getActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLista = (ListView) findViewById(R.id.left_drawer);
drawerControl = new DrawerControl(this);
drawerControl.setDrawerItens();
adapterCustomDrawer = new AdapterDrawerItem(this,drawerControl);
mDrawerLista.setAdapter(adapterCustomDrawer);
mDrawerLista.setOnItemClickListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_abrir,
R.string.drawer_fechar) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle("A");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("B");
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if(savedInstanceState == null) {
selectItem(0);
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
public void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentClienteNovo();
break;
default:
break;
}
android.app.FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerLayout.closeDrawer(mDrawerLista);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
<强> fragment_cliente_novo.xml 强>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<EditText android:id="@+id/editText_cliente_nome" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_nome" android:inputType="textPersonName">
<requestFocus/>
</EditText>
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:text="@string/cliente_tel" android:textAppearance="?android:attr/textAppearanceMedium"/>
<View android:id="@+id/view_linha" android:layout_width="wrap_content" android:layout_height="1dp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:background="@color/Black"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/editText_cliente_tel_cel1" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_tel_cel1" android:inputType="phone"/>
<Spinner android:id="@+id/spinner_clinte_tel_cel_operadora1" android:layout_width="130dp" android:layout_height="33dp" android:layout_marginLeft="3dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/editText_cliente_tel_cel2" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_tel_cel2" android:inputType="phone"/>
<Spinner android:id="@+id/spinner_cliente_tel_cel_operadora2" android:layout_width="130dp" android:layout_height="32dp" android:layout_marginLeft="3dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp"/>
</LinearLayout>
<EditText android:id="@+id/editText_cliente_tel_casa" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_tel_casa" android:inputType="phone"/>
<EditText android:id="@+id/editText_cliente_tel_tabalho" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_tel_trabalho" android:inputType="phone"/>
<TextView android:id="@+id/TextView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:text="@string/cliente_trabalho" android:textAppearance="?android:attr/textAppearanceMedium"/>
<View android:id="@+id/View_linha2" android:layout_width="wrap_content" android:layout_height="1dp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:background="@color/Black"/>
<EditText android:id="@+id/editText_cliente_trabalho_empresa" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_trabalho_empresa"/>
<EditText android:id="@+id/editText_cliente_trabalho_ocupacao" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_trabalho_ocupacao"/>
<EditText android:id="@+id/editText_cliente_trabalho_cidade" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_trabalho_cidade"/>
<EditText android:id="@+id/editText_cliente_trabalho_bairro" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_trabalho_bairro"/>
<EditText android:id="@+id/editText_cliente_trabalho_endereco" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_trabalho_endereco"/>
<TextView android:id="@+id/TextView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:text="@string/cliente_residencia" android:textAppearance="?android:attr/textAppearanceMedium"/>
<View android:id="@+id/View01" android:layout_width="wrap_content" android:layout_height="1dp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:background="@color/Black"/>
<EditText android:id="@+id/EditText_cliente_trabalho_cidade" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_trabalho_cidade"/>
<EditText android:id="@+id/EditText_trabalho_bairro" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_trabalho_bairro"/>
<EditText android:id="@+id/EditText_cliente_trabalho_endereco" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/cliente_trabalho_endereco"/>
</LinearLayout>
</ScrollView>
<强> FragmentClienteNovo.java 强>
package recode.mygv.view;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentClienteNovo extends Fragment {
public FragmentClienteNovo(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cliente_novo, container,
false);
setHasOptionsMenu(true);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_cliente_novo, menu);
}
}