Android TextVIew没有';不出现或不出现';不要添加到线性布局中

Android TextVIew没有';不出现或不出现';不要添加到线性布局中,android,android-layout,textview,Android,Android Layout,Textview,这是主要活动的代码。我在addJoke()方法中添加了TextView,但它没有出现在LinearLayout中。请解决这个问题 package edu.calpoly.android.lab2; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.KeyEvent; import

这是主要活动的代码。我在addJoke()方法中添加了TextView,但它没有出现在LinearLayout中。请解决这个问题

package edu.calpoly.android.lab2;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdvancedJokeList extends Activity {
    private static int i=0;
    //protected String m_strAuthorName;
    protected ArrayList<Joke> m_arrJokeList=new ArrayList<Joke>();
    //protected JokeListAdapter m_jokeAdapter;

    /**
     * ViewGroup used for maintaining a list of Views that each display Jokes.
     **/
    protected LinearLayout m_vwJokeLayout;
    protected EditText m_vwJokeEditText;
    protected Button m_vwJokeButton;
    protected int m_nDarkColor;
    protected int m_nLightColor;
    /**
     * Filter Options Submenu constants
     */
    protected static final int FILTER_OPTIONS = 1;
    protected static final int LIKE = Menu.FIRST + 1;
    protected static final int DISLIKE = Menu.FIRST + 2;
    protected static final int UNRATED = Menu.FIRST + 3;
    protected static final int SHOW_ALL = Menu.FIRST + 4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initLayout();
        String[] jokestring=getResources().getStringArray(R.array.jokeList);    
        for(String str : jokestring) 
        {
                Joke j=new Joke();
                j.setJoke(str);
                addJoke(j);
        }
        initAddJokeListeners();
    }

    protected void addJokeImplementation(){
        String strJoke=m_vwJokeEditText.getText().toString().trim();
        if(!strJoke.equals(""))
        {
        Joke joke=new Joke();
        joke.setJoke(strJoke);
        addJoke(joke);
        }
    }

    protected void initLayout() {
        setContentView(R.layout.advanced);
        m_vwJokeLayout=(LinearLayout)findViewById(R.id.jokeListViewGroup);
        m_vwJokeEditText=(EditText)findViewById(R.id.newJokeEditText);
        m_vwJokeButton=(Button)findViewById(R.id.addJokeButton);
    }

    protected void initAddJokeListeners() {
        // TODO
        m_vwJokeEditText.setOnKeyListener(new OnKeyListener(){
            @Override
            public boolean onKey(View v,int keyCOde, KeyEvent event){
                if(event.getAction()==KeyEvent.ACTION_DOWN)
                {
                    if(keyCOde==KeyEvent.KEYCODE_DPAD_CENTER)
                    {
                        addJokeImplementation();
                    }
                }
                return false;
            }

        });
        m_vwJokeButton.setOnClickListener(new OnClickListener() {
            @Override
        public void onClick(View view) {
        //Implement code to add a new joke here...
                addJokeImplementation();
            }
        });
    }

    protected void addJoke(Joke joke) {
         if(!m_arrJokeList.contains(joke))
          {
            m_arrJokeList.add(joke);
            //I also added textview here this one also
            //doesn't appear in Emulator layout, wondering whats wrong?;
            TextView TV=new TextView(this);
            TV.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            TV.setText(joke.toString());
            m_vwJokeLayout.addView(TV);
            m_nDarkColor=getResources().getColor(R.color.dark);
            m_nLightColor=getResources().getColor(R.color.light);       
            if(i==0)
            {
                TV.setBackgroundColor(m_nLightColor);
                i=1;
            }
            else
            {
                TV.setBackgroundColor(m_nDarkColor);
                i=0;
            }
            m_vwJokeEditText.setText("");
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);
          }
    }         

    /**
     * Method used to retrieve Jokes from online server. The getJoke script
     * takes a single optional parameter, which should be encode in "UTF-8".
     * This parameter allows tells script to only retrieve Jokes whose author
     * name matches the value in the parameter.
     * 
     * param-1) "author": The author of the joke.
     * 
     * URL: http://simexusa.com/aac/getJokes.php?
     * 
     */
    protected void getJokesFromServer() {
        // TODO
    }

    /**
     * This method uploads a single Joke to the server. This method should test
     * the response from the server and display success or failure to the user
     * via a Toast Notification
     * 
     * The addJoke script on the server requires two parameters, both of which
     * should be encode in "UTF-8":
     * 
     * param-1) "joke": The text of the joke.
     * 
     * param-2) "author": The author of the joke.
     * 
     * URL: http://simexusa.com/aac/addJoke.php?
     * 
     * @param joke
     *            The Joke to be uploaded to the server.
     * 
     */
    protected void uploadJokeToServer(Joke joke) {
        // TODO
    }

}         
包edu.calpoly.android.lab2;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.KeyEvent;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.view.OnKeyListener;
导入android.view.ViewGroup.LayoutParams;
导入android.view.inputmethod.InputMethodManager;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公开课高级小丑扩展活动{
私有静态int i=0;
//受保护字符串m_strAuthorName;
受保护的ArrayList m_arrJokeList=新ArrayList();
//受保护的Jokelistatadapter m_Jokedapter;
/**
*用于维护每个视图都显示的视图列表的视图组。
**/
受保护的线性布局m_vwJokeLayout;
受保护的编辑文本m_vwJokeEditText;
受保护按钮m_vwJokeButton;
受保护的int m_nDarkColor;
受保护的国际灯光颜色;
/**
*过滤器选项子菜单常量
*/
受保护的静态最终整数筛选器_选项=1;
受保护的静态最终整型=Menu.FIRST+1;
受保护的静态最终int=Menu.FIRST+2;
受保护的静态最终整数未分级=Menu.FIRST+3;
受保护的静态最终整数显示\u ALL=Menu.FIRST+4;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
initLayout();
字符串[]jokestring=getResources().getStringArray(R.array.jokeList);
用于(字符串str:jokestring)
{
笑话j=新笑话();
j、 setJoke(str);
addJoke(j);
}
initAddJokeListeners();
}
受保护的void addJokeImplementation(){
字符串strJoke=m_vwJokeEditText.getText().toString().trim();
如果(!strJoke.equals(“”)
{
笑话(新笑话);
笑话。设置笑话(strJoke);
addJoke(笑话);
}
}
受保护的void initLayout(){
setContentView(R.layout.advanced);
m_vwJokeLayout=(线性布局)findViewById(R.id.jokeListViewGroup);
m_vwJokeEditText=(EditText)findViewById(R.id.newJokeEditText);
m_vwJokeButton=(按钮)findViewById(R.id.addJokeButton);
}
受保护的void initAddJokeListeners(){
//待办事项
m_vwJokeEditText.setOnKeyListener(新的OnKeyListener(){
@凌驾
公共布尔onKey(视图v、int keyCOde、KeyEvent事件){
if(event.getAction()==KeyEvent.ACTION\u向下)
{
if(keyCOde==KeyEvent.keyCOde\u DPAD\u CENTER)
{
addJokeImplementation();
}
}
返回false;
}
});
m_vwJokeButton.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图){
//实现代码以在此处添加新笑话。。。
addJokeImplementation();
}
});
}
受保护的无效添加笑话(笑话笑话){
如果(!m_arrJokeList.contains(笑话))
{
m_arrJokeList.添加(笑话);
//我也在这里添加了textview这个也
//没有出现在模拟器布局中,不知道出了什么问题?;
TextView TV=新的TextView(此);
TV.setLayoutParams(新的LayoutParams(
LayoutParams.FILL\u父级,
LayoutParams.WRAP_内容);
TV.setText(笑话.toString());
m_vwJokeLayout.addView(电视);
m_nDarkColor=getResources().getColor(R.color.dark);
m_nLightColor=getResources().getColor(R.color.light);
如果(i==0)
{
TV.setBackgroundColor(m_nLightColor);
i=1;
}
其他的
{
TV.setBackgroundColor(m_nDarkColor);
i=0;
}
m_vwJokeEditText.setText(“”);
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(),0);
}
}         
/**
*用于从联机服务器检索笑话的方法
*接受一个可选参数,该参数应以“UTF-8”编码。
*此参数允许告诉脚本仅检索其作者的笑话
*名称与参数中的值匹配。
* 
*param-1“作者”:笑话的作者。
* 
*网址:http://simexusa.com/aac/getJokes.php?
* 
*/
受保护的void getJokesFromServer(){
//待办事项
}
/**
*此方法将单个笑话上载到服务器。此方法应进行测试
*服务器的响应,并向用户显示成功或失败
*通过敬酒通知
* 
*服务器上的addJoke脚本需要两个参数,这两个参数
*应编码为“UTF-8”:
* 
*param-1“笑话”:笑话的文本。
* 
*param-2“作者”:笑话的作者。
* 
*网址:http://simexusa.com/aac/addJoke.php?
* 
*@param笑话
*要上载到服务器的笑话。
* 
*/
受保护的void uploadJokeToServer(笑话){
//待办事项
}
}         
这里是advanced.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/addJokeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Joke"
            />
        <EditText
            android:id="@+id/newJokeEditText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Joke"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/jokeListViewGroup"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

    </LinearLayout>
</LinearLayout>


更新:稍微修改了代码,添加了Layout.params(),但仍然没有出现TextView;您需要直接或通过使用布局参数设置电视视图的高度和宽度,然后在完成此操作后,需要调用setContentView