1

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

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

预先感谢您的回复!

4

1 回答 1

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);
}
于 2013-11-05T13:34:44.397 回答