How to link a button to other xml in Android?

advertisements

How to link a button in Android ?

for example in the Home page there are 2 buttons : 'Item1 & item2'

and i need to put in the code so i can click the 'Item' button and the Item1 data will be displayed ...

this is my current code on Activity.java :

    package com.example.myshops;

    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.app.ActionBar;
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Display;
    import android.view.WindowManager;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.os.Build;

    public class MainActivity extends Activity implements OnClickListener  {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            FragmentManager fragmentManager=getFragmentManager();
            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            Home_fragment hf=new Home_fragment();
            fragmentTransaction.replace(android.R.id.content, hf);
            fragmentTransaction.commit();

            setContentView(R.layout.activity_main);

            Button item1Click = (Button)findViewById(R.id.Item1Button);
            itemClick1.setOnClickListener(this);

            Button item2Click = (Button)findViewById(R.id.Item2Button);
            itemClick2.setOnClickListener(this);

            Button item3Click = (Button)findViewById(R.id.Item3Button);
            itemClick3.setOnClickListener(this);

            Calendar c = Calendar.getInstance();
            System.out.println("Current time => "+c.getTime());

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formattedDate = df.format(c.getTime());
            // formattedDate have current date/time
            Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();

          // Now we display formattedDate value in TextView
            TextView txtView = new TextView(this);
            txtView.setText("Current Date and Time : "+formattedDate);
            txtView.setGravity(Gravity.CENTER);
            txtView.setTextSize(20);
            setContentView(txtView);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @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_main, container, false);
                return rootView;
            }

            public void item1Click(View view) {
                FragmentManager fragmentManager=getFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                Item1_fragment hf=new Item1_fragment();
                fragmentTransaction.replace(android.R.id.content, hf);
                fragmentTransaction.commit();
            }   

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

 switch(v.getId())
      {
       case R.id.Item1Button:

         break;
       case R.id.Item2Button:

         break;
       case R.id.Item3Button:

         break;
       }

        }

        }

my home.xml (for example) :

    <RelativeLayout 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"
        tools:context="com.example.myshops.MainActivity"
        tools:ignore="MergeRootFrame" >

        <Button
            android:id="@+id/Item2Button"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/Item2Button"
            android:onClick="item2Click"
            android:text="@string/Item2" />

        <Button
            android:id="@+id/Item3Button"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/Item2Button"
            android:onClick="item3Click"
            android:text="@string/Item3" />

        <Button
            android:id="@+id/Item1Button"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:onClick="item1Click"
            android:text="@string/Item1" />

        <Button
            android:id="@+id/PrevButton"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="@string/Previous" />

        <Button
            android:id="@+id/HomeButton"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="@string/Home" />

        <Button
            android:id="@+id/NextButton"
            android:layout_width="85dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="@string/Next" />

    </RelativeLayout>

error in my logcats :

04-11 15:56:54.915: D/dalvikvm(547): Not late-enabling CheckJNI (already on)
04-11 15:56:56.145: D/AndroidRuntime(547): Shutting down VM
04-11 15:56:56.155: W/dalvikvm(547): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-11 15:56:56.165: E/AndroidRuntime(547): FATAL EXCEPTION: main
04-11 15:56:56.165: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s0237600_diary/com.example.myshops.MainActivity}: java.lang.NullPointerException
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.os.Looper.loop(Looper.java:137)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-11 15:56:56.165: E/AndroidRuntime(547):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 15:56:56.165: E/AndroidRuntime(547):  at java.lang.reflect.Method.invoke(Method.java:511)
04-11 15:56:56.165: E/AndroidRuntime(547):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-11 15:56:56.165: E/AndroidRuntime(547):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-11 15:56:56.165: E/AndroidRuntime(547):  at dalvik.system.NativeStart.main(Native Method)
04-11 15:56:56.165: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
04-11 15:56:56.165: E/AndroidRuntime(547):  at com.example.myshops.MainActivity.onCreate(MainActivity.java:44)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.app.Activity.performCreate(Activity.java:4465)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-11 15:56:56.165: E/AndroidRuntime(547):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-11 15:56:56.165: E/AndroidRuntime(547):  ... 11 more
04-11 15:56:58.955: I/Process(547): Sending signal. PID: 547 SIG: 9
04-11 15:57:02.035: D/AndroidRuntime(565): Shutting down VM
04-11 15:57:02.035: W/dalvikvm(565): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-11 15:57:02.055: E/AndroidRuntime(565): FATAL EXCEPTION: main
04-11 15:57:02.055: E/AndroidRuntime(565): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myshops/com.example.myshops.MainActivity}: java.lang.NullPointerException
04-11 15:57:02.055: E/AndroidRuntime(565):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-11 15:57:02.055: E/AndroidRuntime(565):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-11 15:57:02.055: E/AndroidRuntime(565):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-11 15:57:02.055: E/AndroidRuntime(565):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-11 15:57:02.055: E/AndroidRuntime(565):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 15:57:02.055: E/AndroidRuntime(565):  at android.os.Looper.loop(Looper.java:137)
04-11 15:57:02.055: E/AndroidRuntime(565):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-11 15:57:02.055: E/AndroidRuntime(565):  at java.lang.reflect.Method.invokeNative(Native Method)


You should either define the click listener in code or in the xml not both. Remove one of them and the other should work.