ImageView Intent动作

时间:2014-05-19 19:48:45

标签: android android-intent imageview

Hii我遇到了一些愚蠢的错误,但相信我,我经历了这个,我没有得到导致我的应用程序崩溃的错误,请通过点击imageview帮我打开一个活动。

它没有打开以下活动并给我错误。如何在活动加载之前打开imageview。

// Menu.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;


public class Menu extends Activity {

    ImageView status,contact,submit,day;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = (ImageView) findViewById(R.id.status_id);
        day = (ImageView) findViewById(R.id.day_id);
        submit = (ImageView) findViewById(R.id.submit_id);
        contact = (ImageView) findViewById(R.id.contact_id);

        setContentView(R.layout.menu);


        status.setOnClickListener(new OnClickListener()
        {
                public void onClick(View v)
                 {  
                    Intent  intent = new Intent(Menu.this,StatusMenu.class);
                    startActivity(intent);

                    }

        });


        day.setOnClickListener(new OnClickListener()
        {
                public void onClick(View v)
                 {  
                    Intent  intent = new Intent(Menu.this,StatusMenu.class);
                    startActivity(intent);

                    }

        });

        submit.setOnClickListener(new OnClickListener()
        {
                public void onClick(View v)
                 {  
                    Intent  intent = new Intent(Menu.this,StatusMenu.class);
                    startActivity(intent);

                    }

        });

        contact.setOnClickListener(new OnClickListener()
        {
                public void onClick(View v)
                 {  
                    Intent  intent = new Intent(Menu.this,StatusMenu.class);
                    startActivity(intent);

                    }

        });

    }






}

// logcat的

05-20 01:10:36.812: D/gralloc_goldfish(643): Emulator without GPU emulation detected.
05-20 01:10:40.133: D/dalvikvm(643): GC_CONCURRENT freed 478K, 9% free 32086K/34951K, paused 31ms+6ms
05-20 01:10:40.563: D/dalvikvm(643): GC_CONCURRENT freed 5K, 3% free 34002K/34951K, paused 27ms+9ms
05-20 01:10:40.822: D/dalvikvm(643): GC_FOR_ALLOC freed 3K, 1% free 35278K/35591K, paused 61ms
05-20 01:10:41.042: D/dalvikvm(643): GC_FOR_ALLOC freed 1K, 1% free 36562K/36871K, paused 48ms
05-20 01:10:41.134: D/AndroidRuntime(643): Shutting down VM
05-20 01:10:41.134: W/dalvikvm(643): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-20 01:10:41.153: E/AndroidRuntime(643): FATAL EXCEPTION: main
05-20 01:10:41.153: E/AndroidRuntime(643): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.status.what/com.status.what.Menu}: java.lang.NullPointerException
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.os.Looper.loop(Looper.java:137)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.app.ActivityThread.main(ActivityThread.java:4340)
05-20 01:10:41.153: E/AndroidRuntime(643):  at java.lang.reflect.Method.invokeNative(Native Method)
05-20 01:10:41.153: E/AndroidRuntime(643):  at java.lang.reflect.Method.invoke(Method.java:511)
05-20 01:10:41.153: E/AndroidRuntime(643):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-20 01:10:41.153: E/AndroidRuntime(643):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-20 01:10:41.153: E/AndroidRuntime(643):  at dalvik.system.NativeStart.main(Native Method)
05-20 01:10:41.153: E/AndroidRuntime(643): Caused by: java.lang.NullPointerException
05-20 01:10:41.153: E/AndroidRuntime(643):  at com.status.what.Menu.onCreate(Menu.java:26)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.app.Activity.performCreate(Activity.java:4465)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-20 01:10:41.153: E/AndroidRuntime(643):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
05-20 01:10:41.153: E/AndroidRuntime(643):  ... 11 more
05-20 01:15:41.312: I/Process(643): Sending signal. PID: 643 SIG: 9

// menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="-40dp"
    android:background="@drawable/green" >


    <ScrollView 

    android:scrollbars="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   android:id="@+id/ScrollView">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_gravity="center_vertical"
    android:orientation="vertical">




    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <ImageView
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:id="@+id/status_id"
            android:src="@drawable/status_form" />



       <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:textSize="15sp"
        android:textColor="#FFFFFF"
        android:id="@+id/satus_text"
        android:text="    Status Outfits" />

    </LinearLayout>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:paddingTop="10dp" >
        <ImageView
            android:id="@+id/submit_id"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/day_id"
            android:paddingRight="10dp"
            android:src="@drawable/submit_form" /> 


        <ImageView
            android:id="@+id/day_id"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/day_form" />

    </RelativeLayout>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:paddingTop="2dp" >

       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textSize="15sp"
        android:textColor="#FFFFFF"
        android:id="@+id/day_text"
        android:text="    Status of the Day" />



       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textSize="15sp"
        android:textColor="#FFFFFF"
        android:id="@+id/submit_text"
        android:text=" Share Your Status   " />

    </RelativeLayout>


    <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="10dp"
    android:orientation="vertical">

        <ImageView
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:id="@+id/contact_id"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/contact_form" />

      <TextView
          android:id="@+id/contact_text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_gravity="center"
          android:layout_marginTop="-15dp"
          android:text="Connect with Developer"
          android:textColor="#FFFFFF"
          android:textSize="15sp" />

        </LinearLayout>



   </LinearLayout>
</ScrollView>
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

findViewById()会返回一个视图,如果它存在于您setContentView()中提供的布局中,否则会返回null以及发生的事情。

在实例化imageViews之前设置内容视图。

更改此部分:

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

    status = (ImageView) findViewById(R.id.status_id);
    day = (ImageView) findViewById(R.id.day_id);
    submit = (ImageView) findViewById(R.id.submit_id);
    contact = (ImageView) findViewById(R.id.contact_id);
    setContentView(R.layout.menu); 

到此:

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

    setContentView(R.layout.menu); //set the contentView before finding Views using findViewByID 

    status = (ImageView) findViewById(R.id.status_id);
    day = (ImageView) findViewById(R.id.day_id);
    submit = (ImageView) findViewById(R.id.submit_id);
    contact = (ImageView) findViewById(R.id.contact_id);

答案 1 :(得分:0)

您应该将setContentView(R.layout.menu)放在super.onCreate(savedInstanceState)下面并尝试

答案 2 :(得分:0)

您必须加载布局并获取对该视图的引用。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu); //* this line must be here!:)
        status = (ImageView) findViewById(R.id.status_id);
        day = (ImageView) findViewById(R.id.day_id);
        submit = (ImageView) findViewById(R.id.submit_id);
        contact = (ImageView) findViewById(R.id.contact_id);

... ... ...