gpt4 book ai didi

Android: "extends BaseActivity"没有执行 BaseActivity 的方法

转载 作者:行者123 更新时间:2023-11-30 02:44:45 26 4
gpt4 key购买 nike

我尝试了两次让滑动抽屉出现在所有 Activity 中。因此,我构建了一个扩展我的子类 MainActivity 的 BaseActivity。我用了this组合布局的代码。这很好用,两种布局都出现在我的 child Activity 中。我为我的抽屉和一些 onItemClick Listenere 等实现了一个 ListView 。但是这些都没有在我的子类中执行。我有一个空抽屉。奇怪的是,我在 BaseActivity 的任何方法中放入的每一个 toast 都出现在屏幕上。

activity_base.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/act_content"
android:layout_width="match_parent"
android:layout_height="200dp" >
</FrameLayout>

<android.support.v4.widget.DrawerLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.trialar.mtb_werkstatt.BaseActivity" >

<ListView
android:id="@+id/drawerList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#FF8800"
android:divider="@null" >
</ListView>
</android.support.v4.widget.DrawerLayout>

</FrameLayout>

BaseActivity.java

public class BaseActivity extends ActionBarActivity implements
OnItemClickListener {

private DrawerLayout drawerLayout;
private ListView listView;

private ActionBarDrawerToggle drawerListener;
private MyAdapter myAdapter;

protected FrameLayout fullLayout;
protected FrameLayout actContent;

@Override
public void setContentView(final int layoutResID) {
fullLayout = (FrameLayout) getLayoutInflater().inflate(
R.layout.activity_base, null); // Your base layout here
actContent = (FrameLayout) fullLayout.findViewById(R.id.act_content);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_base);
listView = (ListView) findViewById(R.id.drawerList);
myAdapter = new MyAdapter(this);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(this);

drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub

}

@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub

}
};
drawerLayout.setDrawerListener(drawerListener);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}

@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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (drawerListener.onOptionsItemSelected(item)) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
selectItem(arg2);
Toast.makeText(BaseActivity.this,
myAdapter.getItem(arg2) + " angeklickt: " + arg2,
Toast.LENGTH_LONG).show();
switch (arg2) {
case 0:
startActivity(new Intent(this, MainActivity.class));
break;
case 1:
startActivity(new Intent(this, Teileliste.class));
break;
case 2:
startActivity(new Intent(this, Reinigung.class));
break;
default:
}

}

public void selectItem(int position) {
// TODO Auto-generated method stub
listView.setItemChecked(position, true);

}

public void setTitle(String title) {
// TODO Auto-generated method stub
getSupportActionBar().setTitle(title);
}

class MyAdapter extends BaseAdapter {
private Context context;
String[] mainCatSites;
int[] images = { R.drawable.ic_werkstatt, R.drawable.ic_teileliste,
R.drawable.ic_reinigung };

public MyAdapter(Context context) {
this.context = context;
mainCatSites = context.getResources().getStringArray(
R.array.mainCat);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mainCatSites.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mainCatSites[arg0];
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View row = null;
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_row, arg2, false);
} else {
row = arg1;
}
TextView titleTextView = (TextView) row
.findViewById(R.id.tvRowList);
ImageView titleImageView = (ImageView) row
.findViewById(R.id.imageView1);
titleTextView.setText(mainCatSites[arg0]);
titleImageView.setImageResource(images[arg0]);
return row;
}

}

}

MainActivity.java

    public class MainActivity extends BaseActivity {

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

}

}

最佳答案

您仍在调用 super.setContentView() 和 THEN setContentView()。这意味着您正在膨胀 R.layout.activity_base 并检索其布局,之后您将整个布局替换为 activity_main。现在 setContentView() 再次膨胀 R.layout.activity_base,这次没有从 activity_base 初始化 View 。基本上,变量“listView”等引用您替换的布局的 View 。

要解决此问题,您需要将 BaseActivity 中的 onCreate() 替换为类似内容

protected final void initViews() { ... }

并移除里面的 super.onCreate() 和 super.setContentView() 。最后,只需添加 initViews();下面 setContentView(R.layout.activity_main);

这将确保将正确的 View 保存在您的变量中。

关于Android: "extends BaseActivity"没有执行 BaseActivity 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com