【问题标题】:Set all the views on an Activity programmatically以编程方式设置 Activity 上的所有视图
【发布时间】:2017-09-03 20:38:31
【问题描述】:

首先,我从 Android 开始,所以也许我在问一个愚蠢的问题。哦,对不起我的英语。

我有一个Activity,它在启动时会收到一个Object(在上一个活动的Intent 中),我想在Java 文件中设置多个视图(ImageViewTextViews)。我一直在寻找方法,这就是我认为我需要的。

XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/party"
tools:context=".activities.PartyActivity">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".activities.PartyActivity"
    android:id="@+id/head">

    <!--Party-->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/photo"/>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".activities.PartyActivity">

        <!--Dia/hora-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/date"/>
        <!--Dirección-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/currentCity"
            android:id="@+id/direction"/>
        <!--Precio-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/currentCity"
            android:id="@+id/price"/>
    </RelativeLayout>
</RelativeLayout>
    <!--Descripcion-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/head"
        android:id="@+id/description"/>

    <!--Mapa-->

这是类:

public class PartyActivity extends Activity {
private RelativeLayout partyLayout;
private Party party;
private ImageView photoView;
private TextView dateView;
private TextView directionVew;
private TextView priceView;
private TextView descriptionView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_party);

    partyLayout = (RelativeLayout)findViewById(R.id.party);
    party = (Party)getIntent().getExtras().getSerializable("Party");
    setParty();

}

private void setParty(){
    partyLayout.removeAllViews();

    photoView = (ImageView)findViewById(R.id.photo);
    dateView = (TextView)findViewById(R.id.date);
    directionVew = (TextView)findViewById(R.id.direction);
    priceView = (TextView)findViewById(R.id.price);
    descriptionView = (TextView)findViewById(R.id.description);

    photoView.setImageBitmap(party.getPhoto());
    dateView.setText(Session.getInstance().getDateFormat().format(party.getDate().getTime()));
    directionVew.setText(party.getDirection());
    priceView.setText(party.getPrice().toString());
    descriptionView.setText(party.getDescripcion());

    setContentView(photoView);
    setContentView(dateView);
    setContentView(directionVew);
    setContentView(priceView);
    setContentView(descriptionView);

}

当我尝试进入活动时,应用程序关闭。我已经调试过了,Party 对象已经被正确接收,但是在setParty 方法中它抛出了这个异常:

 04-08 14:21:08.619 14680-14680/trebolete.keloke E/AndroidRuntime: FATAL EXCEPTION: main
 Process: trebolete.keloke, PID: 14680 java.lang.RuntimeException: Unable to start activity ComponentInfo{trebolete.keloke/trebolete.keloke.activities.PartyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                   at android.os.Handler.dispatchMessage(Handler.java:102)
                   at android.os.Looper.loop(Looper.java:148)
                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                   at java.lang.reflect.Method.invoke(Native Method)
                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                  at trebolete.keloke.activities.PartyActivity.setParty(PartyActivity.java:46)
                  at trebolete.keloke.activities.PartyActivity.onCreate(PartyActivity.java:33)
                  at android.app.Activity.performCreate(Activity.java:6237)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

如果我删除partyLayout.removeAllViews(); 行,它会抛出这个异常:

04-09 13:47:44.101 2576-2576/trebolete.keloke E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: trebolete.keloke, PID: 2576
                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{trebolete.keloke/trebolete.keloke.activities.PartyActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                at android.os.Looper.loop(Looper.java:148)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                             Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                at android.view.ViewGroup.addViewInner(ViewGroup.java:4309)
                                                                at android.view.ViewGroup.addView(ViewGroup.java:4145)
                                                                at android.view.ViewGroup.addView(ViewGroup.java:4117)
                                                                at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:423)
                                                                at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:404)
                                                                at android.app.Activity.setContentView(Activity.java:2186)
                                                                at trebolete.keloke.activities.PartyActivity.setParty(PartyActivity.java:52)
                                                                at trebolete.keloke.activities.PartyActivity.onCreate(PartyActivity.java:33)
                                                                at android.app.Activity.performCreate(Activity.java:6237)
                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

有什么帮助吗?

【问题讨论】:

  • 删除partyLayout.removeAllViews();

标签: android android-activity layout view


【解决方案1】:

因为您已经在onCreate() 函数中设置了父视图。

setContentView(R.layout.activity_party); 将为当前活动设置父布局。您已经在 onCreate() 活动功能中对此进行了初始化。

所有这些视图(

photoView,   dateView,    directionVew,
      priceView ,    descriptionView

) 是 activity_party 的子布局。 您无需对这些视图执行setContentView() 这些视图将自动显示您已在以下代码行中更新的更新数据:

 photoView.setImageBitmap(party.getPhoto());
    dateView.setText(Session.getInstance().getDateFormat().format(party.getDate().getTime()));
    directionVew.setText(party.getDirection());
    priceView.setText(party.getPrice().toString());
    descriptionView.setText(party.getDescripcion());

【讨论】:

    【解决方案2】:

    由于您调用了partyLayout.removeAllViews();,您从布局中删除了所有视图。这就是 findViewById() 返回 null 的原因。

    【讨论】:

    • 感谢您的回答,我已经用您的解决方案编辑了问题,但它不起作用:(
    • 另外​您应该删除所有后续的setContentView()调用。
    猜你喜欢
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2019-07-10
    相关资源
    最近更新 更多