android fragmentmanager 界面报错,Android在fragment中使用listview导致界面闪退

我写了一个Activity,

然后通过点击底部菜单切换三个fragment,

我在第一个fragment中使用了一个listview,

然后就会导致每次进到这个activity界面就会闪退,

去掉listview.setAdapter(adapter)的语句后

界面就不闪退了,代码如下:

主Activity

package com.example.mr4gon.uiapp;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.design.widget.BottomNavigationView;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.view.MenuItem;

import android.view.View;

import android.widget.ListView;

import android.widget.TextView;

import com.example.mr4gon.adapter.WorkAdapter;

import com.example.mr4gon.entity.Work;

import com.example.mr4gon.fragment.HomeFragment;

import com.example.mr4gon.fragment.InfoFragment;

import com.example.mr4gon.fragment.MineFragment;

import java.util.ArrayList;

import java.util.List;

public class HomeActivity extends AppCompatActivity {

private List workList=new ArrayList<>();

protected HomeFragment homeFragment=new HomeFragment();

protected MineFragment mineFragment=new MineFragment();

protected InfoFragment infoFragment=new InfoFragment();

private TextView mTextMessage;

//底部菜单栏切换事件

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener

= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override

public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {

case R.id.navigation_home:

mTextMessage.setText("主页");

getSupportFragmentManager().beginTransaction().show(homeFragment)

.hide(mineFragment).hide(infoFragment).commit();

return true;

case R.id.navigation_dashboard:

mTextMessage.setText("个人中心");

getSupportFragmentManager().beginTransaction().hide(homeFragment)

.show(mineFragment).hide(infoFragment).commit();

return true;

case R.id.navigation_notifications:

mTextMessage.setText("通知");

getSupportFragmentManager().beginTransaction().hide(homeFragment)

.hide(mineFragment).show(infoFragment).commit();

return true;

}

return false;

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_home);

//菜单栏切换

mTextMessage = (TextView) findViewById(R.id.message);

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);

navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

//fragment切换

this.getSupportFragmentManager().beginTransaction()

.add(R.id.main_content,homeFragment)

.add(R.id.main_content,mineFragment).hide(mineFragment)

.add(R.id.main_content,infoFragment).hide(infoFragment)

.commit();

//初始化伪数据

initWork();

//主页适配器

WorkAdapter adapter=new WorkAdapter(HomeActivity.this,R.layout.work_item,workList);

ListView listview=(ListView)findViewById(R.id.mylistview);

listview.setAdapter(adapter);

}

//页面点击事件

public void onClick(View v) {

switch (v.getId()){

//点击退出按钮

case R.id.exit_btn:

AlertDialog.Builder dialog =new AlertDialog.Builder(HomeActivity.this);

dialog.setTitle("提示");

dialog.setMessage("确认退出登录?");

dialog.setCancelable(false);

dialog.setPositiveButton("是", new DialogInterface.OnClickListener(){

@Override

public void onClick(DialogInterface dialog, int which) {

finish();;

}

});

dialog.setNegativeButton("否",new DialogInterface.OnClickListener(){

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();

}

});

dialog.show();

break;

//点击修改资料

case R.id.modify_btn:

Intent intent =new Intent(HomeActivity.this,ModifyActivity.class);

startActivity(intent);

break;

//点击我的作品

case R.id.mywork_btn:

Intent intent1=new Intent(HomeActivity.this,WorkActicity.class);

intent1.putExtra("worktype","1");

startActivity(intent1);

break;

//点击我的改编

case R.id.myadapt_btn:

Intent intent2=new Intent(HomeActivity.this,WorkActicity.class);

intent2.putExtra("worktype","2");

startActivity(intent2);

break;

//点击发表作品

case R.id.publish_btn:

Intent intent3=new Intent(HomeActivity.this,PublishActivity.class);

startActivity(intent3);

default:

break;

}

}

//循环制作伪数据

private void initWork(){

for (int i=0;i<2;i++){

Work work=new Work(i,"番茄炒蛋", "无名氏",R.drawable.egg, "简单,美味", 9.0);

workList.add(work);

}

}

}

Fragment类

package com.example.mr4gon.fragment;

import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ListView;

import com.example.mr4gon.uiapp.R;

/**

* Created by Mr4gon on 2017/9/15.

*/

public class HomeFragment extends Fragment {

public View listview;

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.home_fragment, container, false);

listview = (ListView) v.findViewById(R.id.mylistview);

return v;

}

@Override

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

Log.d("d1", "onActivityCreated: ");

super.onActivityCreated(savedInstanceState);

}

}

主Activity布局

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.mr4gon.uiapp.HomeActivity">

android:layout_width="match_parent"

android:layout_height="60dp"

android:background="@drawable/title"

android:elevation="3dp">

android:id="@+id/message"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="主页"

android:layout_centerInParent="true"

android:layout_centerHorizontal="true"

android:textColor="#fff"

android:textSize="18dp"/>

android:id="@+id/main_content"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1">

android:id="@+id/navigation"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="bottom"

android:background="?android:attr/windowBackground"

app:menu="@menu/navigation" />

Fragment布局

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/mylistview"

android:layout_width="match_parent"

android:layout_height="match_parent">

Adapter

package com.example.mr4gon.adapter;

import android.content.Context;

import android.support.annotation.NonNull;

import android.support.annotation.Nullable;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import com.example.mr4gon.entity.Work;

import com.example.mr4gon.uiapp.R;

import java.util.List;

/**

* Created by Mr4gon on 2017/9/15.

*/

public class WorkAdapter extends ArrayAdapter {

private int resoutId;

public WorkAdapter(Context context, int textViewResourceId, List objects){

super(context,textViewResourceId,objects);

resoutId=textViewResourceId;

}

@NonNull

@Override

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

Work work=getItem(position);

View view= LayoutInflater.from(getContext()).inflate(resoutId,parent,false);

ImageView w_picture=(ImageView)view.findViewById(R.id.w_picture);

TextView w_name=(TextView)view.findViewById(R.id.w_name);

TextView w_author=(TextView)view.findViewById(R.id.w_author);

TextView keywords=(TextView)view.findViewById(R.id.keywords);

TextView rate=(TextView)view.findViewById(R.id.rate);

w_picture.setImageResource(work.getW_picture());

w_name.setText(work.getW_name());

w_author.setText(work.getW_author());

keywords.setText(work.getKeywords());

rate.setText(""+work.getRate());

return view;

}

}

Workitem

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:padding="10dp"

android:layout_margin="15dp">

android:id="@+id/w_picture"

android:layout_width="match_parent"

android:layout_height="100dp"

android:layout_weight="4" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="5"

android:orientation="vertical"

android:layout_marginLeft="20dp">

android:id="@+id/w_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="作品名"

android:layout_weight="1"

android:textSize="20dp"/>

android:id="@+id/w_author"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="作者名"

android:layout_weight="1"/>

android:id="@+id/keywords"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="关键字"

android:layout_weight="1"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_weight="1">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="评分:"

android:textSize="18dp"/>

android:id="@+id/rate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="9.7"

android:textColor="#ff0000"

android:textSize="18dp"/>

报错信息

09-23 19:45:36.208 4220-4220/com.example.mr4gon.uiapp E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.mr4gon.uiapp, PID: 4220

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mr4gon.uiapp/com.example.mr4gon.uiapp.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2733)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2807)

at android.app.ActivityThread.access$900(ActivityThread.java:192)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1629)

at android.os.Handler.dispatchMessage(Handler.java:111)

at android.os.Looper.loop(Looper.java:192)

at android.app.ActivityThread.main(ActivityThread.java:5917)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1099)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:865)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

at com.example.mr4gon.uiapp.HomeActivity.onCreate(HomeActivity.java:83)

at android.app.Activity.performCreate(Activity.java:6200)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2680)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2807)

at android.app.ActivityThread.access$900(ActivityThread.java:192)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1629)

at android.os.Handler.dispatchMessage(Handler.java:111)

at android.os.Looper.loop(Looper.java:192)

at android.app.ActivityThread.main(ActivityThread.java:5917)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1099)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:865)

09-23 19:45:36.233 4220-4220/com.example.mr4gon.uiapp I/Process: Sending signal. PID: 4220 SIG: 9

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值