0

Hello I've got an app and I need help regarding ImageButton.

Code:

protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.social);

        ImageButton img = (ImageButton) findViewById(R.id.imageView1);
        img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                startActivity(new Intent(social.this,facebook.class));
            }   
        });
    }
}

manifest file

   android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity android:name="ana"></activity>
         <activity android:name="facebook"></activity>
          <activity android:name="instagram"></activity>
           <activity android:name="pinterest"></activity>
            <activity android:name="twitter"></activity>
             <activity android:name="youtube"></activity>
              <activity android:name="social"></activity>


    </application>

</manifest>

Layout file

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignTop="@+id/imageButton1"
    android:src="@drawable/fbf" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:src="@drawable/aas" />

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_alignTop="@+id/imageButton1"
    android:src="@drawable/yoube" />

<ImageButton
    android:id="@+id/imageButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton8"
    android:layout_below="@+id/imageButton3"
    android:layout_marginTop="19dp"
    android:src="@drawable/ss" />

<ImageButton
    android:id="@+id/imageButton5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton1"
    android:layout_alignTop="@+id/imageButton4"
    android:src="@drawable/mysp" />

<ImageButton
    android:id="@+id/imageButton6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton2"
    android:layout_alignTop="@+id/imageButton7"
    android:src="@drawable/sc" />

<ImageButton
    android:id="@+id/imageButton7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton5"
    android:layout_below="@+id/imageButton5"
    android:layout_marginTop="14dp"
    android:src="@drawable/tumblr" />

<ImageButton
    android:id="@+id/imageButton8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton3"
    android:layout_alignTop="@+id/imageButton7"
    android:src="@drawable/justinlogo" />

<ImageButton
    android:id="@+id/imageButton9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton2"
    android:layout_alignTop="@+id/imageButton5"
    android:src="@drawable/insta" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton6"
    android:layout_below="@+id/imageButton6"
    android:layout_marginTop="24dp"
    android:src="@drawable/fbf" />

What I want to do is, when i click the ImageButton it should open facebook class. I made it with normal button but, unlike normal button, its not working with ImageButton.

Its giving error "application stopped" ! someone help me ? Thanks all.

**LOGCAT REPORT ** enter image description here

MacSTD
  • 7
  • 1
  • 9

2 Answers2

0

Add dot . prefix(dot means your package name. It's more short type of declaration.) in your activities in manifest file,i.e.

<activity android:name=".ana"></activity>
<activity android:name=".facebook"></activity>
<activity android:name=".instagram"></activity>
<activity android:name=".pinterest"></activity>
<activity android:name=".twitter"></activity>
<activity android:name=".youtube"></activity>
<activity android:name=".social"></activity>

And clean your project.

For more info see How to add activity in manifest file

And change

    ImageButton img = (ImageButton) findViewById(R.id.imageView1);

to

ImageButton img = (ImageButton) findViewById(R.id.imageButton1);

Because in social layout file ImageButton id is imageButton1

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

ITS NOT A PROPER WAY TO DEFINE CLASS IN ACTIVITY.........

There are some proper mathods to declare Activity Class in MANIFEST If all class files are in same package (main package) define classes like below..

<activity android:name=".ana"></activity>
<activity android:name=".facebook"></activity>
<activity android:name=".instagram"></activity>
<activity android:name=".pinterest"></activity>
<activity android:name=".twitter"></activity>
<activity android:name=".youtube"></activity>
<activity android:name=".social"></activity>

if its in another package define it with package names like

<activity android:name="com.example.package2.social"></activity>

and one another thing that always define CLASS WITH FIRST LETTER CAPITAL..... like Ana or Facebook or Social

Edit :

There is Difference between ImageButton and ImageView.. you are calling wrong ID...

ImageButton img = (ImageButton) findViewById(R.id.imageView1); <-- calling imageview for imagebutton...

it shouldbe like this,,

ImageButton img = (ImageButton) findViewById(R.id.imageButton1);<-- calling imagebutton...
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40