0

我有在纵向模式下完美运行的 xml。但是我又为横向创建了一个文件夹作为 layout-land 并向其中添加了相同的 XML 并根据横向模式进行了一些更改,但是当我运行我的应用程序时出现以下错误。

03-18 09:30:59.276: E/AndroidRuntime(5753): FATAL EXCEPTION: main
    03-18 09:30:59.276: E/AndroidRuntime(5753): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.netserv.Pungry/com.netserv.Pungry.RestaurantDetails}: java.lang.NullPointerException
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.os.Looper.loop(Looper.java:137)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at java.lang.reflect.Method.invoke(Method.java:511)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at dalvik.system.NativeStart.main(Native Method)
    03-18 09:30:59.276: E/AndroidRuntime(5753): Caused by: java.lang.NullPointerException
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at com.netserv.Pungry.RestaurantDetails.onCreate(RestaurantDetails.java:44)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.app.Activity.performCreate(Activity.java:5104)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    03-18 09:30:59.276: E/AndroidRuntime(5753):     ... 11 more

这是 XML。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="vertical"
    android:weightSum="1.0" >

  <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:background="@color/headerbgcolor"
      android:weightSum="1.0" 
      >  

     <Button
            android:id="@+id/restaurant_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/back" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.6"
            android:gravity="center"
            android:text="@string/restauran_details" />

        <Button
            android:id="@+id/restaurant_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/home" />
     </LinearLayout>

  <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      >

      <Button
          android:id="@+id/Refresh"
          android:layout_width="wrap_content"
          android:layout_height="20dp"
          android:gravity="left"
          android:text="@string/refresh" />
  </RelativeLayout>

  <LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:weightSum="1">

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:text="@string/restaurant_name" 
        />

     <TextView
         android:layout_width="fill_parent"
         android:layout_height="match_parent"
         android:text="@string/restaurant_address" />

     </LinearLayout>
     <ScrollView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" >
     <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:weightSum="1" >
     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/restaurant_rating"
        android:layout_weight="0.1"  />

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/restaurant_timings" 
        android:layout_weight="0.1"  />

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cost_for_two" 
        android:layout_weight="0.1" />

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/restaurant_cuisine" 
        android:layout_weight="0.1" />

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/restaurant_location" 
        android:layout_weight="0.1" />

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/restaurant_city" 
        android:layout_weight="0.1" />

</LinearLayout>
</ScrollView>

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:text="@string/restaurant_phone"
        />

<!--      <RelativeLayout -->
<!--         android:layout_width="fill_parent" -->
<!--         android:layout_height="wrap_content" > -->

<!--         <ImageView -->
<!--             android:id="@+id/imageView1" -->
<!--             android:layout_width="fill_parent" -->
<!--             android:layout_height="wrap_content" -->
<!--             android:layout_alignParentBottom="true" -->
<!--             android:background="#808000" -->
<!--             android:src="@drawable/btn_bg_cuisine" /> -->
<!--     </RelativeLayout> -->


</LinearLayout>

这是我的 RestaurantDetails 课程

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restaurantdetails);
        btnback = (Button) findViewById(R.id.back);
        btnhome = (Button) findViewById(R.id.restaurant_home);
        btnRefresh = (Button) findViewById(R.id.Refresh);
        btnRefresh.setVisibility(View.GONE);
        Restaurant_name = (TextView) findViewById(R.id.Restaurant_name);
        Restaurant_add = (TextView) findViewById(R.id.Restaurant_add);
        Restaurant_costfortwo = (TextView) findViewById(R.id.Restaurant_cstfortwo);
        Restaurant_rating = (TextView) findViewById(R.id.Restaurant_rating);
        Restaurant_timings = (TextView) findViewById(R.id.Restaurant_timings);
        Restaurant_phone = (TextView) findViewById(R.id.Restaurant_phone);
        Restaurant_phone.setAutoLinkMask(Linkify.PHONE_NUMBERS);
        Restaurant_phone.setLinksClickable(true);

        Restaurant_Locationname = (TextView) findViewById(R.id.Restaurant_location);
        Restaurant_cityname = (TextView) findViewById(R.id.Restaurant_city);
        Restaurant_cuisinename = (TextView) findViewById(R.id.Restaurant_cuisine);
        imgfooter = (ImageView) findViewById(R.id.footerimage);

        btnhome.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(RestaurantDetails.this, Home.class));
            }
        });

        Restaurant_name.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).restaurant_name);
        Restaurant_add.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).address);
        Restaurant_costfortwo.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).costoftwo);
        Restaurant_rating.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).rating);
        Restaurant_timings.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).timings);
        Restaurant_phone.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).Phone);
        Restaurant_Locationname.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).Location_name);
        Restaurant_cityname.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).city_name);
        Restaurant_cuisinename.setText(GlobVariables.restaurant_response
                .get(GlobVariables.RestaurantName).Cuisine_name);

我能知道我在做什么错吗?..谢谢:)

4

1 回答 1

2

中的错误btnback = (Button) findViewById(R.id.back);

布局 xml中没有按钮具有 id back

我想你想用restaurant_back

btnback = (Button) findViewById(R.id.restaurant_back);.

此外,所有 TextView 都应该在 xml 布局中具有 id。

Restaurant_name = (TextView) findViewById(R.id.Restaurant_name);
        Restaurant_add = (TextView) findViewById(R.id.Restaurant_add);
        Restaurant_costfortwo = (TextView) findViewById(R.id.Restaurant_cstfortwo);
        Restaurant_rating = (TextView) findViewById(R.id.Restaurant_rating);
        Restaurant_timings = (TextView) findViewById(R.id.Restaurant_timings);
        Restaurant_phone = (TextView) findViewById(R.id.Restaurant_phone);
        Restaurant_Locationname = (TextView) findViewById(R.id.Restaurant_location);
        Restaurant_cityname = (TextView) findViewById(R.id.Restaurant_city);
        Restaurant_cuisinename = (TextView) findViewById(R.id.Restaurant_cuisine);

    and Image View


imgfooter = (ImageView) findViewById(R.id.footerimage);

为所有在 xml 布局中添加这个android:id="@+id/...."

<....
             android:id="@+id/...."
            .
            .
            .
            />
于 2013-03-18T09:54:43.087 回答