【问题标题】:Call requires api level 11. What to do when i get this error?调用需要 api 级别 11。当我收到此错误时该怎么办?
【发布时间】:2012-10-26 09:49:22
【问题描述】:

我正在使用此代码

public class MainActivity extends Activity {

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


    String hallo = "blabla " + "jojo " + "lol " + "\n" + "doj " + "drasl " + "\n"; 
    InputStream is = new ByteArrayInputStream(hallo.getBytes());
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    String line;
    try {
        while ((line = in.readLine()) != null) {
            String[] RowData = line.split(" ");
            String eitt = RowData[0];
            String tvo = RowData[1];

            TextView textView = new TextView(this);
            textView.setText(line);
            textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT
           ,LayoutParams.WRAP_CONTENT));
            linearLayout.addView(textView);
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    }
}

有了这个xml代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        </LinearLayout>

</ScrollView>

我想将字符串的这些部分插入到表格布局中,这样当我的文本被打印出来时,它们就会出现在漂亮的列中。它的工作方式是逐行打印字符串,而不是将其放入列中。知道我不知道字符串中有多少行也很重要,所以我不能只制作 4 个 textview 框,因为可能有 50 og 一百行,所以我需要制作一个 i表格视图中文本视图框的数量,i 是行数。但我总是得到这个错误,调用需要 api 级别 11,我使用的是 api 级别 10。错误是因为这条线而出现的

textView.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT
           ,LayoutParams.WRAP_CONTENT));

我可以使用什么样的参数布局选项来让它工作。

【问题讨论】:

  • 这很奇怪,因为 View.setLayoutParams()LayoutParams(int,int) 在 API 11 之前就已经存在了。您可以导入不同的 LayoutParams 吗?试试这个:textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT ,ViewGroup.LayoutParams.WRAP_CONTENT));
  • 这很奇怪,就像我不能使用 FILL_PARENT,eclipse 说 FILL_PARENT 已被弃用。而且我在代码中看不到任何错误。
  • 你的最小和目标 sdk 是什么??
  • minsdkversion = 8 和 targetsdkversion = 15 @Girish
  • 尝试使用常量值意味着Constant Value: -1 (0xffffffff) FILL_PARENT 所以使用-1 代替LayoutParams.FILL_PARENT developer.android.com/reference/android/view/…

标签: android android-layout textview layoutparams


【解决方案1】:

您没有显示导入,但您可能使用了错误的 LayoutParams 导入(如 meowmeow 建议的那样)。看起来您可能想改用以下导入。

import android.view.ViewGroup.LayoutParams;

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。仔细检查您的导入,看看您没有导入不合适的 LayoutParams。我最终导入了一些随机的东西,比如 Actionbar.LayoutParams 应该是 LinearLayout.LayoutParams。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-11
      • 2021-03-29
      • 2021-09-26
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多