从Fragment类打开活动时,该活动显示为空白

时间:2019-06-25 12:25:45

标签: android android-layout android-fragments android-intent

我有一个片段活动,它是一个列表视图。当用户单击列表视图中的项目时,它将打开另一个活动。新活动已加载,但显示为空白。

我尝试重新创建活动,但不能解决问题。我是Android和Java编码的新手。该应用程序运行正常。我可以使用“后退”按钮返回到列表,然后单击另一项。结果是一样的。

我尝试了所有可能的方法,但无法修复。有人可以帮我吗?

片段活动

package com.ornisoft.bloodbankapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.ornisoft.bloodbankapp.DataAdapter.MessageDataAdapter;
import com.ornisoft.bloodbankapp.DataModel.MessageDataModel;

import java.util.ArrayList;

public class MessageFragment  extends Fragment {
    DatabaseHelper db;
    ArrayList<MessageDataModel> MessageDataModelArrayList;
    MessageDataAdapter MessageDataAdapter;

    Context context;
    ListView lvMessages;



    public MessageFragment(){}
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.listview_message, container, false);
        context = container.getContext();

        lvMessages = view.findViewById(R.id.lvMessages);
        lvMessages.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent  intent =  new Intent(getActivity(), MessageActivity.class);
                //intent.putExtra("id",l);
                startActivity(intent);
            }
        });
        db = new DatabaseHelper(context);
        loadMessages();
        return view;
    }
    public void loadMessages(){
        MessageDataModelArrayList = db.getMessages();
        MessageDataAdapter = new MessageDataAdapter(context,MessageDataModelArrayList);
        lvMessages.setAdapter(MessageDataAdapter);
        MessageDataAdapter.notifyDataSetChanged();
    }
}

消息活动

package com.ornisoft.bloodbankapp;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MessageActivity extends AppCompatActivity {
    DatabaseHelper db;
    int messageID;
    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_message);

        db = new DatabaseHelper(this);
        Toast.makeText(MessageActivity.this, "Hello world " , Toast.LENGTH_SHORT).show();

        /*messageID= Integer.parseInt(getIntent().getStringExtra("id"));
        MessageModel messageModel = db.getMessage(messageID);*/
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MessageActivity">


    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

谢谢

1 个答案:

答案 0 :(得分:0)

我发现了错误。我在MessageActivity上使用了错误的create类。

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) 

当我将其更改为

protected void onCreate(Bundle savedInstanceState)

问题已解决,并且可以正常运行。

谢谢大家。