以编程方式设置活动的所有视图

时间:2017-04-08 14:42:19

标签: android android-activity layout view

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

我有Activity在启动时收到Object(在上一个活动的Intent中)并且我想设置多个视图(ImageView和{{1在Java文件中。我已经找到了这样做的方法,这就是我认为我需要的。

XML文件:

TextViews

这是班级:

<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();

任何帮助?

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分:0)

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