java - addView 运行时异常

标签 java android

尝试通过添加新 Activity 和移动到该 Activity 的按钮来编辑 Android 教程应用程序,但在按下“发送”按钮后,我不断收到运行时异常错误(应用程序崩溃)。研究过许多类似的问题,但对其他人有效的解决方案对我并不适用。

DisplayMessageActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myfirstapp.DisplayMessageActivity"
tools:ignore="MergeRootFrame" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:onClick="switchActivity"
        android:text="@string/next" />
</LinearLayout>

DisplayMessageActivity.java

package com.example.myfirstapp;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        ViewGroup layout = (ViewGroup) findViewById(R.id.container);
        layout.addView(textView);

        // Set the text view as the activity layout
        setContentView(R.layout.activity_display_message);
        //setContentView(textView);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() { }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_display_message,
                      container, false);
              return rootView;
        }
    }
    public void switchActivity (View view) {
        Intent maxIntent = new Intent(this, Selection.class);
        startActivity(maxIntent);
    }


}

希望你能发现我的错误。 提前致谢!

最佳答案

你应该在 setContentView(R.layout.activity_display_message); 之前移动

ViewGroup layout = (ViewGroup) findViewById(R.id.container);

像这样,

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

    setContentView(R.layout.activity_display_message);

    ViewGroup layout = (ViewGroup) findViewById(R.id.container);
   .............
   ............
   ...........
 }

也可以尝试这种方式在您的布局中添加 View 。

LinearLayout layout= (LinearLayout)findViewById(R.id.container);
layout.addView(textView);

关于java - addView 运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24735586/

相关文章:

java - 如何使用 jarjar 从默认包中重新打包 .class 文件?

android - 如何通过java在远程网络服务器上为android应用程序创建数据库

android - 构建 android tesseract-android-tools 库而不获取 tesslib 和 leptonlib

java - SWT 转换错误

java - 是否有用于解析 Excel 2007 文件的 Java 开源库?

java - 泛型:为什么我不能将 Object 作为我的 Collection 的参数?

java - 如何将文档对象转换为输入源?

android: Third party library jar uses a library I plan to use,导致dalvik错误 "Unable to execute dex: Multiple dex files define"

android - 取消选择 ListView 中的选定项目

java - 从 Android 网站获取信息