错误获取其他活动中的项目位置

时间:2016-08-31 16:22:15

标签: android listview android-intent android-activity

嗨,我有一个listview,我想采取litview的位置,但我得到下一个错误:

FATAL EXCEPTION: main
    Process: com.amordeltarot.horoscopo.horoscoposdiarios, PID: 3465
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.amordeltarot.horoscopo.horoscoposdiarios/com.amordeltarot.horoscopo.horoscoposdiarios.horosocopovista}: 
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
    at com.amordeltarot.horoscopo.horoscoposdiarios.horosocopovista.<init>(horosocopovista.java:14)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是我的MainActivity.java类:

public class MainActivity extends AppCompatActivity {

    ListView signos;
    String zodiaco[] = {"Aries", "Tauro", "Geminis", "Cáncer", "Leo", "Virgo","Libra","Escorpio","Sagitario","Capricornio","Acuario","Piscis" };

    String zodiacofechas[] = {"21 de marzo - 20 de abril", "21 de abril - 20 de mayo", "21 de mayo - 20 de junio", "21 de junio - 22 de julio", "23 de julio - 22 de agosto", "23 de agosto - 20 de septiembre","21 de septiembre - 20 de octubre","21 de octubre - 20 de noviembre","21 de noviembre - 20 de diciembre","21 de diciembre - 20 de enero","21 de enero - 20 de febrero","21 de febrero - 20 de marzo" };

    Integer[] imgid={
        R.drawable.aries,
        R.drawable.taurus,
        R.drawable.gemini,
        R.drawable.cancer,
        R.drawable.leo,
        R.drawable.virgo,
        R.drawable.libra,
        R.drawable.scorpio,
        R.drawable.sagittarius,
        R.drawable.capricornus,
        R.drawable.aquarius,
        R.drawable.pisces
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        signos = (ListView)findViewById(R.id.listView);
        final adaptadornuevo arrayAdapter =
                new adaptadornuevo(this, zodiaco,imgid,zodiacofechas);
        signos.setAdapter(arrayAdapter);

        signos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent myIntent = new Intent(v.getContext(), horosocopovista.class);
                myIntent.putExtra("position", position);
                startActivity(myIntent);
            }
        });
    }
}

这就是HoroscopeVista:

public class HorosocopeVista extends AppCompatActivity {

    int prePosition = getIntent().getIntExtra("position", 0);

    Integer[]imgid2={
            R.drawable.aries,
            R.drawable.taurus,
            R.drawable.gemini,
            R.drawable.cancer,
            R.drawable.leo,
            R.drawable.virgo,
            R.drawable.libra,
            R.drawable.scorpio,
            R.drawable.sagittarius,
            R.drawable.capricornus,
            R.drawable.aquarius,
            R.drawable.pisces

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horosocopovista);
        ImageView imageView = (ImageView) findViewById(R.id.icongrande);
        imageView.setImageResource(imgid2[prePosition]);
    }

}

这是android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.amordeltarot.horoscopo.horoscoposdiarios">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".horosocopovista"
            android:label="@string/title_activity_horosocopovista"
            android:parentActivityName=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.amordeltarot.horoscopo.horoscoposdiarios.MainActivity" />
        </activity>
    </application>

</manifest>

我希望你能帮助我,因为我不知道我做错了什么

1 个答案:

答案 0 :(得分:2)

尝试写作

NaN
调用int prePosition = getIntent().getIntExtra("position", 0);

后,在onCreate内{p>更好