gpt4 book ai didi

java - 无法创建 CardView fragment

转载 作者:行者123 更新时间:2023-11-29 03:06:17 25 4
gpt4 key购买 nike

我不知道我的代码有什么问题。

我的应用程序正在使用抽屉导航。有 4 个不同的 fragment ,其中一个是 cardview fragment ,我之前已将其从 Activity 转换为 fragment 。

cardview fragment 带有红色下划线,表示无法编译的错误。请引用所附图片。

如果我接受 Android Studio 提出的解决方案,它只会解决 CardViewFragment,而其他 3 个 fragment 会出现新问题。

enter image description here

enter image description here

这既是我的主要 Activity ,也是 cardview fragment 。

MainActivity.java

package info.androidhive.materialdesign.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import info.androidhive.materialdesign.R;

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;

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

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

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);

drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);

// display the first navigation drawer view on app launch
displayView(0);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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;
}

if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}

private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new FriendsFragment();
title = getString(R.string.title_friends);
break;
case 2:
fragment = new MessagesFragment();
title = getString(R.string.title_messages);
break;
case 3:
fragment = new CardViewFragment();
title = getString(R.string.title_messages);
break;
default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();

// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
}

CardViewFragment.java

package info.androidhive.materialdesign.activity;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;

import info.androidhive.materialdesign.R;


public class CardViewFragment extends Fragment {

private Toolbar toolbar;
private RecyclerView recyclerView;
private CardView cardView;

private ArrayList<FeddProperties> os_versions;

private RecyclerView.Adapter mAdapter;
// private RecyclerView.LayoutManager mLayoutManager;


private LinearLayout llLayout;
private FragmentActivity faActivity;
@Override
/*protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initContrls();
}*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
faActivity=(FragmentActivity)
super.getActivity();
llLayout=(LinearLayout)
inflater.inflate(R.layout.activity_main, container, false);

initContrls();

return llLayout;
}

private void initContrls() {

//toolbar = (Toolbar) findViewById(R.id.toolbar);
// cardView = (CardView) findViewById(R.id.cardList);
recyclerView = (RecyclerView) llLayout.findViewById(R.id.my_recycler_view);

/*if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Android Versions");

}*/

final String[] versions = {"Alpha", "Beta", "CupCake", "Donut",
"Eclair", "Froyo", "Gingerbread", "Honeycomb",
"Ice Cream Sandwitch", "JellyBean", "KitKat", "LollyPop"};
final int[] icons = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.donut, R.drawable.eclair, R.drawable.froyo, R.drawable.gingerbread, R.drawable.honeycomb, R.drawable.icecream_sandwhich, R.drawable.jellybean, R.drawable.kitkat, R.drawable.lollipop};


os_versions = new ArrayList<FeddProperties>();

for (int i = 0; i < versions.length; i++) {
FeddProperties feed = new FeddProperties();

feed.setTitle(versions[i]);
feed.setThumbnail(icons[i]);
os_versions.add(feed);
}

recyclerView.setHasFixedSize(true);

// ListView
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

//Grid View
// recyclerView.setLayoutManager(new GridLayoutManager(this,2,1,false));

//StaggeredGridView
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,1));

// create an Object for Adapter
mAdapter = new CardViewDataAdapter(os_versions);

// set the adapter object to the Recyclerview
recyclerView.setAdapter(mAdapter);


}


/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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);
}*/
}

最佳答案

在 MainActivity.java 中,你声明了

import android.support.v4.app.Fragment

CardViewFragment.java中导入相同类型的Fragment而不是

 import android.app.Fragment;

因此很明显,在两段代码中使用了相同类型的 fragment 。

关于java - 无法创建 CardView fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32042521/

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