Android ScrollView添加新行

时间:2013-11-05 12:21:47

标签: java android scrollview

我是Android / Java编程的新手,我甚至很难解决这个问题。我只是不知道什么是错的。我将创建一个新的Activity,将Extra添加到Intent并想要在滚动视图中显示字符串!我的应用程序在开始此活动时始终关闭。

        protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_parcelable_text);

    dataTableScrollView = (RelativeLayout) findViewById(R.id.scrollView1);

    Intent intent = getIntent();

    if(savedInstanceState == null){
        dataListView = intent.getStringExtra(MainActivity.EXTRA_PARCELABLE_TEXT)+
                "\n";
    }else{
        dataListView += savedInstanceState.get(PARCELABLE_STRING)+
                "\n";
    }

    setAllDataToListView();
}

private void setAllDataToListView(){
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.
            LAYOUT_INFLATER_SERVICE);

    View newListViewElement = inflater.inflate(R.layout.activity_parcelable_text, null);

    TextView newDataTextView = (TextView) newListViewElement.findViewById(R.id.scrollViewTextView);

    newDataTextView.setText(dataListView);

    dataTableScrollView.addView(newListViewElement, 0);
}

我不知道我做错了什么! 我找不到合适的答案。

提前感谢您的回复!

1 个答案:

答案 0 :(得分:0)

你把活动类放在AndroidManifest.xml中了吗?如果没有像这样添加

<activity android:name="com.project.classname" />

和 尝试将您的代码修改为此

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_parcelable_text);

    dataTableScrollView = (RelativeLayout) findViewById(R.id.scrollView1);

   //Get values for intent when activity started for firsttime
   Bundle extras = getIntent().getExtras(); 
   if (extras != null) 
    {
        //Get dataListView as String 
    dataListView    = extras.getString("dataListView");
    }

   //Get values on orientation 
   if( savedInstanceState != null )
    { 
       dataListView = savedInstanceState.getString("dataListView");
    }


   //Add extra String PARCELABLE_STRING
   dataListView += savedInstanceState.get(PARCELABLE_STRING)+"\n";


    setAllDataToListView();
}

//save info when screen orientation 
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

  outState.putString("dataListView",dataListView);
}   


private void setAllDataToListView(){
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.
            LAYOUT_INFLATER_SERVICE);

    View newListViewElement = inflater.inflate(R.layout.activity_parcelable_text, null);

    TextView newDataTextView = (TextView) newListViewElement.findViewById(R.id.scrollViewTextView);

    newDataTextView.setText(dataListView);

    dataTableScrollView.addView(newListViewElement, 0);
}