Android-列表视图未在新活动中显示

Android-列表视图未在新活动中显示,android,android-activity,android-listview,Android,Android Activity,Android Listview,我对Android和面向对象的代码相当陌生,所以我可能忽略了一些小东西 我有一个活动,其中包含一些自定义的ListView,这些ListView是片段,通过滑动和选项卡进行更改。按特定列表中的特定项几次后,将打开一个新活动并包含另一个ListView 当我运行代码时,我可以从片段StatusFragment.java中打开第二个活动FactoryScreen.java,但是FactoryScreen.java显示为空白活动 我一直在网上论坛上搜寻类似的案例,但没有任何运气。再一次,由于我是新手,

我对Android和面向对象的代码相当陌生,所以我可能忽略了一些小东西

我有一个活动,其中包含一些自定义的ListView,这些ListView是片段,通过滑动和选项卡进行更改。按特定列表中的特定项几次后,将打开一个新活动并包含另一个ListView

当我运行代码时,我可以从片段StatusFragment.java中打开第二个活动FactoryScreen.java,但是FactoryScreen.java显示为空白活动

我一直在网上论坛上搜寻类似的案例,但没有任何运气。再一次,由于我是新手,我不完全确定如何调试它。但是,我注意到,fragment_factory_screen.xml(包含活动布局)完全可以显示带有基本Hello world的TextView项,但显然不是ListView

StatusFragment.java FactoryScreen.java在此处通过意图初始化

    import java.util.ArrayList;

    import com.example.path.R;

    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ListView;
    import android.widget.Toast;

    public class StatusFragment extends Fragment
    {
         private Context mContext;
         private ArrayList<DataItem> data = new ArrayList<DataItem>(); // DataItem contains two strings
         ListView myList;
         int i = 0;

         @Override
         public void onCreate (Bundle savedInstanceState)
         {    
              super.onCreate(savedInstanceState);

              mContext = getActivity(); // Get the context of the Activity and therefore the fragment
              myList = new ListView (mContext); // Create a ListView

              // Fill out elements to go into 'data'
              ...
              // data.add each DataItem variable
              ...
          }

          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
          {
               // Inflate the view
               View rootView = inflater.inflate(R.layout.fragment_status, container, false);

               // (?) Identify myList to be a list to be placed in rootView
               myList=(ListView) rootView.findViewById(android.R.id.list);
               // Create and set the customised adapter to take data (ArrayList<DataItem>) and format
               // each element placed as defined in list_row.xml
               CustomAdapter adapter = new CustomAdapter(mContext, R.layout.list_row, data);
               myList.setAdapter(adapter);

               // When a myList item is clicked, show a Toast.
               myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
               {
                    public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id)
                    {
                         // If Item 1 is clicked 3 times, open new Activity
                     if(id == 1)
                     {
                          i++;
                          if(i == 3)
                          {
                               Intent intent = new Intent(mContext, FactoryScreen.class);
                           i = 0;
                           startActivity(intent);    
                          }
                          else
                          {
                                   ...
                          }
                     }
                     else
                     {
                          ...
                     }
                     }
                 });
             return rootView; // onCreateView must return a View variable if displaying a UI.
         }
     }
要打开FactoryScreen.java活动,不显示ListView

import java.util.ArrayList;

import com.example.baseappwithcalibration.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class FactoryScreen extends Activity {

    private Context mContext;
    private ArrayList<DataItem> data = new ArrayList<DataItem>();
    ListView myList;

    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_factory_screen);

        mContext = this; // Get the context of the Activity and therefore the fragment
        myList = new ListView (mContext); // Create a ListView

        // Create elements to be added to the list
        ...

        // Add elements to the list
        ...
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate the view
        View rootView = inflater.inflate(R.layout.fragment_factory_screen, container, false);

        // (?) Identify myList to be a list to be placed in rootView
        myList=(ListView)findViewById(android.R.id.list);
        // Create and set the customised adapter to take data (ArrayList<DataItem>) and format
        // each element as defined in list_row.xml
        CustomAdapter adapter = new CustomAdapter(mContext, R.layout.list_row, data);
        myList.setAdapter(adapter);

        // When a myList item is clicked, show a Toast.
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id)
            {
                // This is used in the form of Toast.makeText(Context, Text, Duration);
                Toast.makeText(FactoryScreen.this, "This is the description of the parameter.", Toast.LENGTH_SHORT).show();
            }
        });
        return rootView; // onCreateView must return a View variable if displaying a UI.
    }
}
fragment_factory_screen.xml包含FactoryScreen.xml的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical" >

    <ListView 
        android:drawSelectorOnTop="false"
        android:id="@android:id/list"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_width="match_parent" >
    </ListView> 

</LinearLayout>

感谢您的帮助/指导

在FactoryScreen中,在onCreate方法中

您不会在布局中添加listView

如果layout R.layout.fragment_factory_屏幕包含ListView,则可以使用

myList=(ListView)findViewById(myId);
否则,您必须使用代码添加到视图中。

试试这个

在onCreateView中

活动没有名为onCreateViewLayoutInflater充气器、ViewGroup容器、Bundle savedInstanceState的方法。因此,在您的情况下不会执行is函数。这就是ListView不可见的原因。因此,在FactoryScreen活动中,将代码从这个方法onCreateView移动到onCreate


}

它可以工作-谢谢!我以前尝试过类似的方法,但在视图范围方面遇到了问题。奇怪的是他们没有再出现。谢谢你的帮助。我现在已经解决了,我之前尝试过类似的方法,但没有成功。我认为这种方法第一次可能不起作用,因为我在添加元素之前试图创建列表。
myList=(ListView)findViewById(myId);
myList=(ListView)rootView.findViewById(android.R.id.list);
@Override
public void onCreate (Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_factory_screen);

    mContext = this; // Get the context of the Activity and therefore the fragment

    // (?) Identify myList to be a list to be placed in rootView
    myList=(ListView)findViewById(android.R.id.list);

    // Create and set the customised adapter to take data (ArrayList<DataItem>) and format
    // each element as defined in list_row.xml
    CustomAdapter adapter = new CustomAdapter(mContext, R.layout.list_row, data);
    myList.setAdapter(adapter);

    // When a myList item is clicked, show a Toast.
    myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id)
        {
            // This is used in the form of Toast.makeText(Context, Text, Duration);
            Toast.makeText(FactoryScreen.this, "This is the description of the parameter.", Toast.LENGTH_SHORT).show();
        }
    });

}