试图通过添加一个新活动和一个移至该活动的按钮来编辑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);
   .............
   ............
   ...........
 }


并尝试在布局中添加视图。

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

07-28 02:03