0

I have xml which is working perfectly in portrait mode. but i created one more folder for landscape as layout-land and added the same XML into it and made some changes according to landscape mode, but when i run my app i am getting following error.

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

Here is the 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>

Here is my RestaurantDetails class

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

Can i know what's the mistake i am doing?.. Thanks:)

Beginner
  • 1,414
  • 2
  • 21
  • 41

1 Answers1

2

Error in btnback = (Button) findViewById(R.id.back);.

There are no button has id back in layout xml.

I think you want use restaurant_back

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

Also all TextView should have id in xml layout.

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

add for all in xml layout this android:id="@+id/...."

<....
             android:id="@+id/...."
            .
            .
            .
            />
AwadKab
  • 3,054
  • 2
  • 17
  • 17