运行android App错误(致命异常:主进程:PID:14099)

哈姆扎·阿卜杜拉(Hamza Abdullah)

我有以下代码,并且在运行App PLZ时有以下错误:帮助我

主要活动

package com.comp.easybuy.easybuy;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import static android.net.Uri.parse;

public class MainActivity extends Activity {

    private ImageButton callusphone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);
        callusphone=(ImageButton)findViewById(R.id.callus);
        callusphone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent callIntent = new Intent(Intent.ACTION_CALL, parse("tel:+962796880853"));
                startActivity(callIntent);
            }
        });
    }

    public void flick(View view){
        Intent bowsprit =new Intent(Intent.ACTION_VIEW, parse("http://www.facebook.com/ebuy2015/"));
        startActivity(bowsprit);

    }
    public void incline(View view){
        Intent browbeaten =new Intent(Intent.ACTION_VIEW, parse("http://www.instagram.com/easy_buy_1/"));
        startActivity(browbeaten);

    }


    private class ImagePagerAdapter extends PagerAdapter {
        private int[] mImages = new int[] {
                R.drawable.offer4,
                R.drawable.offer5,
                R.drawable.offer6,
        };
        @Override
        public int getCount() {
            return mImages.length;
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = MainActivity.this;
            ImageView imageView = new ImageView(context);
            int padding =context.getResources().
                    getDimensionPixelSize(R.dimen.abc_switch_padding);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(mImages[position]);
            container.addView(imageView, 0);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((ImageView) object);
        }


        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

开机画面

package com.comp.easybuy.easybuy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Splash extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }
                catch(InterruptedException e){
                    e.printStackTrace();
                }
                finally{
                    Intent intent = new Intent(Splash.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        };
        timerThread.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
}

表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.comp.easybuy.easybuy">
    <uses-permission android:name="ANDROID.PERMISSION.CALL_PHONE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Splash"
            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=".MainActivity"
            android:label="@string/app_name"
            android:exported="true">
            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


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

            </intent-filter>
        </activity>
    </application>

</manifest>

主要活动

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:onClick="flick"
        android:src="@drawable/fbook"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="false"
        android:scaleType="center"
        android:contentDescription="facebook Image Button"
        android:layout_alignParentStart="false"
        android:theme="@style/Widget.AppCompat.RatingBar"
        android:visibility="visible" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/insta"
        android:onClick="incline"
        android:src="@drawable/inst"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/imageButton"
        android:layout_alignParentEnd="false"
        android:layout_alignParentStart="false"
        android:scaleType="center"
        android:theme="@style/Widget.AppCompat.RatingBar"
        android:visibility="visible" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/callus"
        android:onClick="callusphone"
        android:src="@drawable/callus"
        android:layout_alignParentBottom="false"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="145dp"
        android:layout_marginRight="145dp"
        android:layout_below="@+id/view_pager"
        android:layout_alignParentEnd="false"
        android:layout_alignParentStart="false"
        android:scaleType="center"
        android:theme="@style/Widget.AppCompat.RatingBar"
        android:visibility="visible" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignWithParentIfMissing="false"
        android:layout_gravity="right|left"
        android:visibility="visible"
        android:background="#ffffff"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/imageButton" />


</RelativeLayout>

并有错误

08-19 09:30:36.123  14099-14099/com.comp.easybuy.easybuy E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.comp.easybuy.easybuy, PID: 14099
    java.lang.IllegalArgumentException: Expected receiver of type com.comp.easybuy.easybuy.MainActivity, but got android.view.ContextThemeWrapper
            at java.lang.reflect.Method.invoke(Native Method)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4448)
            at android.view.View.performClick(View.java:5199)
            at android.view.View$PerformClick.run(View.java:21155)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5415)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
Shishram

首先声明您的图像按钮如下

        imageFb=(ImageButton)findViewById(R.id.imageButton);
        imageInsta=(ImageButton)findViewById(R.id.insta);

和使用 OnClickListener

imageFb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flick(v);
            }
        });
        imageInsta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                incline(v);
            }
        });

现在检查它应该工作!您已经在xml中定义了onClick,但是还没有使用它并且将viewto传递给了方法,因此它使您的代码崩溃了!

最终MainActivity应该看起来像这样!

    package com.comp.easybuy.easybuy;

    import android.app.Activity;
    import android.support.v7.app.AppCompatActivity;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import static android.net.Uri.parse;

    public class MainActivity extends Activity {

        private ImageButton callusphone;
        private ImageButton imageFb;
        private ImageButton imageInsta;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
            ImagePagerAdapter adapter = new ImagePagerAdapter();
            viewPager.setAdapter(adapter);
            callusphone=(ImageButton)findViewById(R.id.callus);

            imageFb=(ImageButton)findViewById(R.id.imageButton);
            imageInsta=(ImageButton)findViewById(R.id.insta);

            callusphone.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent callIntent = new Intent(Intent.ACTION_CALL, parse("tel:+962796880853"));
                    startActivity(callIntent);
                }
            });

            imageFb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    flick(v);
                }
            });
            imageInsta.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    incline(v);
                }
            });

        }

        public void flick(View view){
            Intent bowsprit =new Intent(Intent.ACTION_VIEW, parse("http://www.facebook.com/ebuy2015/"));
            startActivity(bowsprit);

        }
        public void incline(View view){
            Intent browbeaten =new Intent(Intent.ACTION_VIEW, parse("http://www.instagram.com/easy_buy_1/"));
            startActivity(browbeaten);

        }


        private class ImagePagerAdapter extends PagerAdapter {
            private int[] mImages = new int[] {
                    R.drawable.offer4,
                    R.drawable.offer5,
                    R.drawable.offer6,
            };
            @Override
            public int getCount() {
                return mImages.length;
            }
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                Context context = MainActivity.this;
                ImageView imageView = new ImageView(context);
                int padding =context.getResources().
                        getDimensionPixelSize(R.dimen.abc_switch_padding);
                imageView.setPadding(padding, padding, padding, padding);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setImageResource(mImages[position]);
                container.addView(imageView, 0);
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((ImageView) object);
            }


            }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.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();
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章