gpt4 book ai didi

java - addView 运行时异常

转载 作者:行者123 更新时间:2023-11-29 07:47:08 30 4
gpt4 key购买 nike

尝试通过添加新 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/

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