App won't launch or crash when using image button in android

I have just started developing my first app in android.

Here I used an image button.

But when I try to run my app on the device, it doesn't fire the errors that show up in the cat log.

Can someone please help me on how to resolve this error. Thanks in advance. Hosting for your help.

This is the code I used:

ArraysActivity.java

package com.me.array;
//import com.sweans.pus.SingleListItem;

//import com.sweans.pus.SecondScreen;


//import com.me.array.R;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;

public class ArraysActivity extends Activity {
        ListView listView;
         private  ImageButton mybtn;
        ArrayAdapter<String> adapter;

        String[] sujith =new String[]{ "The-Birth", "Menu", "Album",
                        "Events", "Blog", "Press", "Reservation", "MONOMANIA",
                        "Contact" };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mybtn = (ImageButton)findViewById(R.id.imgbtn);
        mybtn.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                Intent nextScreen = new Intent(getApplicationContext(), ZActivity.class);
                startActivity(nextScreen);
            }
        });




         listView = (ListView) findViewById(R.id.mylist);
        adapter = new ArrayAdapter<String>(this,
                                android.R.layout.simple_list_item_1, android.R.id.text1, sujith);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new OnItemClickListener() {

         public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
           //   Intent nextScreen = new Intent(getApplicationContext(), SingleListItem.class);
              // startActivity(new Intent(action));
                 if(position == 0){
                 Intent i = new Intent(ArraysActivity.this, SingleListItem.class);

                 // passing variable
                 i.putExtra("my.package.dataToPass","new");
          //i.putExtra( "int",position);

                 startActivity(i);
                 }
                 else   if(position == 1){
                         Intent i = new Intent(ArraysActivity.this, ListSample.class);

                startActivity(i);
                         }

                 else   if(position == 2){
                         Intent i = new Intent(ArraysActivity.this, MyGridView.class);

                startActivity(i);
                         }
                else    if(position == 4){
                         Intent i = new Intent(ArraysActivity.this, MessageList.class);

                startActivity(i);
                         }
           }
       });
    }
}

main.xml

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

   <!--  <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="278dp"
        android:scaleType="fitXY"
        android:src="@drawable/home_page" /> -->

    <ImageButton
       android:id="@+id/imgbtn"
        android:layout_width="fill_parent"
        android:layout_height="278dp"
        android:scaleType="fitXY"
        android:src="@drawable/home_page"
        android:contentDescription="@string/desc"/>

    <ListView
        android:id="@+id/mylist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </ListView>

</LinearLayout>


Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.me.array"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/icon_andro"
        android:label="@string/app_name" >
        <activity
            android:name=".ArraysActivity"
            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=".SingleListItem"></activity>
              <activity android:name=".menu"></activity>
              <activity android:name=".ListSample"></activity>
              <activity android:name=".SeperatedListAdapter"></activity>
              <activity android:name=".MyGridView"></activity>
                 <activity android:name=".Message"></activity>
                    <activity android:name=".MessageList"></activity>
                 <activity android:name=".BaseFeedParser"></activity>
                 <activity android:name=".SeparatedListAdapter"></activity>
                 <activity android:name=".RssHandler"></activity>
                     <activity android:name=".ZActivity"></activity>
    </application>
                        <uses-permission android:name="android.permission.INTERNET" />
</manifest>

      

The log-cat shows NPE on line 36, i.e. mybtn.setOnClickListener (new OnClickListener () {.

my log code enter image description here

+3


source to share


1 answer


Instead of anonymous onClikListener, try it like this:

mybtn.setOnClickListener(this);

      

Then, at the end of your source, do a listener,

public void onClick(View v)
{
    //do the stuff you want here
}

      

I would replace getApplicationContext () with "this" btw, of course only in this version.

It should work this way I think.



OR, you can try it like this:

Context ctx; //define it as class variable

      

In onCreate:

ctx=this;

      

Replace getApplicationContext () with ctx, I think this is what the NullPointer gives you.

0


source







All Articles