gpt4 book ai didi

java - Android ScrollView 添加新行

转载 作者:行者123 更新时间:2023-12-01 04:12:30 26 4
gpt4 key购买 nike

我是 Android/Java 编程的新手,我什至无法解决这个问题。我只是不明白出了什么问题。我将创建一个新的 Activity ,将额外的内容添加到 Intent 中,并希望在 ScrollView 内显示字符串!我的应用程序在开始此 Activity 时总是关闭。

        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);
}

我只是不明白我做错了什么!我找不到正确的答案。

预先感谢您的回复!

最佳答案

您是否将 Activity 类放入 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);
}

关于java - Android ScrollView 添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19788901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com