第二个活动在Android中产生问题

时间:2015-11-16 08:22:21

标签: android android-intent android-activity

我写了一个android程序。当我点击第一个活动时,第二个活动无法显示。如何使用关键字' final`声明imageview以及如何编写私有方法。拜托,任何人都可以帮我解决我的错误。感谢您的帮助。

SecondActivity.java

public class SecondActivity extends Activity {

    Button btnPrevious= null;
    Button btnNext = null;
    TextView txtTest = null;

    int conditions[];
    String days[];

    // condition imageView initialization together with declaration 
    final ImageView T1C4R4 = (ImageView) findViewById(R.id.T1C4R4);
    final ImageView T2C4R5 = (ImageView) findViewById(R.id.T2C4R5);
    final ImageView T4C4R1 = (ImageView) findViewById(R.id.T4C4R1);
    final ImageView T3C4R1 = (ImageView) findViewById(R.id.T3C4R1);

    // imageView initialization together with declaration

    final ImageView T1C4R1 = (ImageView) findViewById(R.id.T1C4R1); 
    final ImageView T1C2R3 = (ImageView) findViewById(R.id.T1C2R3);
    final ImageView T1C1R4 = (ImageView) findViewById(R.id.T1C1R4);

    final ImageView T2C4R1 = (ImageView) findViewById(R.id.T2C4R1);
    final ImageView T2C2R3 = (ImageView) findViewById(R.id.T2C2R3);
    final ImageView T2C1R5 = (ImageView) findViewById(R.id.T2C1R5);

    final ImageView T4C1R1 = (ImageView) findViewById(R.id.T4C1R1);
    final ImageView T4C2R2 = (ImageView) findViewById(R.id.T4C2R2);
    final ImageView T4C5R5 = (ImageView) findViewById(R.id.T4C5R5);

    final ImageView T3C4R5 = (ImageView) findViewById(R.id.T3C4R5);
    final ImageView T3C2R3 = (ImageView) findViewById(R.id.T3C2R3);
    final ImageView T3C1R1 = (ImageView) findViewById(R.id.T3C1R1);

    final ImageView[][] dressImageView =
        {
            {T1C4R1, T1C2R3, T1C1R4},
            {T2C4R1,T2C2R3, T2C1R5} ,
            {T4C1R1, T4C2R2,T4C5R5},
            {T3C4R5, T3C2R3, T3C1R1}
        }; 

    final ImageView [] dayCell = {T1C4R4 ,T2C4R5, T4C4R1, T3C4R1 };

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

        setContentView(R.layout.second_activity);

        //ImageView Initialization


        btnPrevious =  (Button) findViewById(R.id.btnPrevious2);
        btnNext =  (Button) findViewById(R.id.btnNext2);
        txtTest = (TextView) findViewById(R.id.txtTest2);


        // get the Intent that started this Activity
        Intent in = getIntent();

        // get the Bundle that stores the data of this Activity
        Bundle b = in.getExtras();

        // getting data from bundle
        conditions = b.getIntArray("condition");
        days = b.getStringArray("day");

        // show data to layout
        txtTest.setText("[Condition: " + conditions[0] + ", " + conditions[1] + ", " + conditions[2] + ", " + conditions[3] + "]");

        for (int dc = 0; dc < conditions.length; dc++) {
            dayWeatherCondtion(dc, days[dc]);                   // 0 --- Today
            weatherConditionDress(dc, conditions[dc]);          // 0   10
        }
        btnPrevious.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }

        });

        btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();

                b.putInt("condition", conditions[1]);
                b.putString("day", days[1]);

                // Creating Intent object
                Intent in = new Intent(SecondActivity.this, DetailActivity.class);

                // Storing bundle object into intent
                in.putExtras(b);
                startActivity(in);
            }
        });
    }

    private void weatherConditionDress(int day, int condition) {   // 0 ----  10

        switch(condition){
        case 2: 
        {
            //String dress_array[] = ;
            weatherDress(day, getResources().getStringArray(R.array.five));      //0 ----- 
            break;
        }
        case 5:
        case 6:
        {
            //  String dress_array[] = getResources().getStringArray(R.array.five);
            weatherDress(day, getResources().getStringArray(R.array.five));
            break;
        }
        case 9:
        {
            //  String dress_array[] = getResources().getStringArray(R.array.five);
            weatherDress(day, getResources().getStringArray(R.array.nine));
            break;
        }
        case 11:
        {
            //weatherCondtion(day, getResources().getStringArray(R.string.condition_11));
            //String dress_array[] = getResources().getStringArray(R.array.five);
            //weatherDress(getResources().getStringArray(R.array.five));    
            break;
        }
        case 12:    
        {
            //String dress_array[] = getResources().getStringArray(R.array.five);
            weatherDress(day, getResources().getStringArray(R.array.five)); 
            break;
        }
        case 25:
        {
            //  String dress_array[] = getResources().getStringArray(R.array.five);
            weatherDress(day, getResources().getStringArray(R.array.twentyFive));
            break;
        }
        case 26:
        case 27:    
        case 28:
        case 29:
        case 30:
        case 31:
        {
            //  String dress_array[] = getResources().getStringArray(R.array.five);
            weatherDress(day, getResources().getStringArray(R.array.twentySix));
            break;  
        }
        case 36:
        {
            //  String dress_array[] = getResources().getStringArray(R.array.five);
            weatherDress(day, getResources().getStringArray(R.array.thirtySix));
            break;
        }
        case 37:
        case 38:
        case 39:
        case 44:    
        {
            //  String dress_array[] = getResources().getStringArray(R.array.five);
            weatherDress(day, getResources().getStringArray(R.array.thirtySeven));
            break;  
        }
        }
    }

    private void dayWeatherCondtion(int index, String day) {
        dayCell[index].setImageDrawable(getDrawableByName(this,day));
        Log.d("DAY","Day is " + day);
    }

    private void weatherDress(int day, String[] dress_array) {
        for(int d = 0; d  < dress_array.length; d++ ){
            //ImageView temp = dressImageView[d];
        Log.d("Dress Image", " "+ dressImageView[day][d]);  
        dressImageView[day][d].setImageDrawable(getDrawableByName(this, dress_array[d]));

        }
    }

    public Drawable getDrawableByName(Context context, String name){
        Resources resources = context.getResources();
        final int resourceId = resources.getIdentifier(name, "drawable", 
                context.getPackageName());
        return resources.getDrawable(resourceId);
    }

}

错误:

11-16 20:58:37.532: D/AndroidRuntime(1331): Shutting down VM
11-16 20:58:37.532: W/dalvikvm(1331): threadid=1: thread exiting with uncaught exception (group=0xa6297288)
11-16 20:58:37.532: E/AndroidRuntime(1331): FATAL EXCEPTION: main
11-16 20:58:37.532: E/AndroidRuntime(1331): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.easy2access.weatherapp/com.easy2access.weatherapp.SecondActivity}: java.lang.NullPointerException
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.os.Looper.loop(Looper.java:137)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at java.lang.reflect.Method.invoke(Method.java:511)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at dalvik.system.NativeStart.main(Native Method)
11-16 20:58:37.532: E/AndroidRuntime(1331): Caused by: java.lang.NullPointerException
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.app.Activity.findViewById(Activity.java:1825)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at com.easy2access.weatherapp.SecondActivity.<init>(SecondActivity.java:27)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at java.lang.Class.newInstanceImpl(Native Method)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at java.lang.Class.newInstance(Class.java:1319)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-16 20:58:37.532: E/AndroidRuntime(1331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-16 20:58:37.532: E/AndroidRuntime(1331):     ... 11 more

2 个答案:

答案 0 :(得分:1)

始终在onCreate()中找到您的观点。
  SecondActivity.java

public class SecondActivity extends Activity {

    // Here your Global Declaration.
    final ImageView[][] dressImageView;
    final ImageView[] dayCell;

    Button btnPrevious = null;
    Button btnNext = null;
    TextView txtTest = null;

    int conditions[];
    String days[];

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

        setContentView(R.layout.second_activity);

        // ImageView Initialization
        // change made here.This all ImageView find inside onCreate().

        // condition imageView initialization together with declaration
        final ImageView T1C4R4 = (ImageView) findViewById(R.id.T1C4R4);
        final ImageView T2C4R5 = (ImageView) findViewById(R.id.T2C4R5);
        final ImageView T4C4R1 = (ImageView) findViewById(R.id.T4C4R1);
        final ImageView T3C4R1 = (ImageView) findViewById(R.id.T3C4R1);

        // imageView initialization together with declaration

        final ImageView T1C4R1 = (ImageView) findViewById(R.id.T1C4R1);
        final ImageView T1C2R3 = (ImageView) findViewById(R.id.T1C2R3);
        final ImageView T1C1R4 = (ImageView) findViewById(R.id.T1C1R4);

        final ImageView T2C4R1 = (ImageView) findViewById(R.id.T2C4R1);
        final ImageView T2C2R3 = (ImageView) findViewById(R.id.T2C2R3);
        final ImageView T2C1R5 = (ImageView) findViewById(R.id.T2C1R5);

        final ImageView T4C1R1 = (ImageView) findViewById(R.id.T4C1R1);
        final ImageView T4C2R2 = (ImageView) findViewById(R.id.T4C2R2);
        final ImageView T4C5R5 = (ImageView) findViewById(R.id.T4C5R5);

        final ImageView T3C4R5 = (ImageView) findViewById(R.id.T3C4R5);
        final ImageView T3C2R3 = (ImageView) findViewById(R.id.T3C2R3);
        final ImageView T3C1R1 = (ImageView) findViewById(R.id.T3C1R1);

        dressImageView={{T1C4R1,T1C2R3,T1C1R4},{T2C4R1,T2C2R3,T2C1R5},{T4C1R1,T4C2R2,T4C5R5},{T3C4R5,T3C2R3,T3C1R1}};

        dayCell={T1C4R4,T2C4R5,T4C4R1,T3C4R1};

        btnPrevious = (Button) findViewById(R.id.btnPrevious2);
        btnNext = (Button) findViewById(R.id.btnNext2);
        txtTest = (TextView) findViewById(R.id.txtTest2);

        // get the Intent that started this Activity
        Intent in = getIntent();

        // get the Bundle that stores the data of this Activity
        Bundle b = in.getExtras();

        // getting data from bundle
        conditions = b.getIntArray("condition");
        days = b.getStringArray("day");

        // show data to layout
        txtTest.setText("[Condition: " + conditions[0] + ", " + conditions[1] + ", " + conditions[2] + ", "
                + conditions[3] + "]");

        /*
         * for (int dc = 0; dc < conditions.length; dc++) {
         * dayWeatherCondtion(dc, days[dc]); // 0 --- Today
         * weatherConditionDress(dc, conditions[dc]); // 0 10 }
         */
        btnPrevious.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }

        });

        btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();

                b.putInt("condition", conditions[1]);
                b.putString("day", days[1]);

                // Creating Intent object
                Intent in = new Intent(SecondActivity.this, DetailActivity.class);

                // Storing bundle object into intent
                in.putExtras(b);
                startActivity(in);
            }
        });
    }
/*private void weatherConditionDress(int day, int condition) {   // 0 ----  10

    switch(condition){
    case 2: 
    {
        //String dress_array[] = ;
        weatherDress(day, getResources().getStringArray(R.array.five));      //0 ----- 
        break;
    }
    case 5:
    case 6:
    {
        //  String dress_array[] = getResources().getStringArray(R.array.five);
        weatherDress(day, getResources().getStringArray(R.array.five));
        break;
    }
    case 9:
    {
        //  String dress_array[] = getResources().getStringArray(R.array.five);
        weatherDress(day, getResources().getStringArray(R.array.nine));
        break;
    }
    case 11:
    {
        //weatherCondtion(day, getResources().getStringArray(R.string.condition_11));
        //String dress_array[] = getResources().getStringArray(R.array.five);
        //weatherDress(getResources().getStringArray(R.array.five));    
        break;
    }
    case 12:    
    {
        //String dress_array[] = getResources().getStringArray(R.array.five);
        weatherDress(day, getResources().getStringArray(R.array.five)); 
        break;
    }
    case 25:
    {
        //  String dress_array[] = getResources().getStringArray(R.array.five);
        weatherDress(day, getResources().getStringArray(R.array.twentyFive));
        break;
    }
    case 26:
    case 27:    
    case 28:
    case 29:
    case 30:
    case 31:
    {
        //  String dress_array[] = getResources().getStringArray(R.array.five);
        weatherDress(day, getResources().getStringArray(R.array.twentySix));
        break;  
    }
    case 36:
    {
        //  String dress_array[] = getResources().getStringArray(R.array.five);
        weatherDress(day, getResources().getStringArray(R.array.thirtySix));
        break;
    }
    case 37:
    case 38:
    case 39:
    case 44:    
    {
        //  String dress_array[] = getResources().getStringArray(R.array.five);
        weatherDress(day, getResources().getStringArray(R.array.thirtySeven));
        break;  
    }
    }
}

private void dayWeatherCondtion(int index, String day) {
    dayCell[index].setImageDrawable(getDrawableByName(this,day));
    Log.d("DAY","Day is " + day);
}

private void weatherDress(int day, String[] dress_array) {
    for(int d = 0; d  < dress_array.length; d++ ){
        //ImageView temp = dressImageView[d];
    Log.d("Dress Image", " "+ dressImageView[day][d]);  
    dressImageView[day][d].setImageDrawable(getDrawableByName(this, dress_array[d]));

    }
}
//
public Drawable getDrawableByName(Context context, String name){
    Resources resources = context.getResources();
    final int resourceId = resources.getIdentifier(name, "drawable", 
            context.getPackageName());
    return resources.getDrawable(resourceId);
}
}

答案 1 :(得分:1)

在setcontentview

之后移动视图的初始化
 public class SecondActivity extends Activity {

        Button btnPrevious= null;
        Button btnNext = null;
        TextView txtTest = null;

        int conditions[];
        String days[];


        /*final ImageView[][] dressImageView =
            {
                {T1C4R1, T1C2R3, T1C1R4},
                {T2C4R1,T2C2R3, T2C1R5} ,
                {T4C1R1, T4C2R2,T4C5R5},
                {T3C4R5, T3C2R3, T3C1R1}
            }; 

        final ImageView [] dayCell = {T1C4R4 ,T2C4R5, T4C4R1, T3C4R1 };*/

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

            setContentView(R.layout.second_activity);

            //ImageView Initialization

// move initialisation here 

 // condition imageView initialisation together with declaration 
        final ImageView T1C4R4 = (ImageView) findViewById(R.id.T1C4R4);
        final ImageView T2C4R5 = (ImageView) findViewById(R.id.T2C4R5);
        final ImageView T4C4R1 = (ImageView) findViewById(R.id.T4C4R1);
        final ImageView T3C4R1 = (ImageView) findViewById(R.id.T3C4R1);

        // imageView initialization together with declaration

        final ImageView T1C4R1 = (ImageView) findViewById(R.id.T1C4R1); 
        final ImageView T1C2R3 = (ImageView) findViewById(R.id.T1C2R3);
        final ImageView T1C1R4 = (ImageView) findViewById(R.id.T1C1R4);

        final ImageView T2C4R1 = (ImageView) findViewById(R.id.T2C4R1);
        final ImageView T2C2R3 = (ImageView) findViewById(R.id.T2C2R3);
        final ImageView T2C1R5 = (ImageView) findViewById(R.id.T2C1R5);

        final ImageView T4C1R1 = (ImageView) findViewById(R.id.T4C1R1);
        final ImageView T4C2R2 = (ImageView) findViewById(R.id.T4C2R2);
        final ImageView T4C5R5 = (ImageView) findViewById(R.id.T4C5R5);

        final ImageView T3C4R5 = (ImageView) findViewById(R.id.T3C4R5);
        final ImageView T3C2R3 = (ImageView) findViewById(R.id.T3C2R3);
        final ImageView T3C1R1 = (ImageView) findViewById(R.id.T3C1R1);



            btnPrevious =  (Button) findViewById(R.id.btnPrevious2);
            btnNext =  (Button) findViewById(R.id.btnNext2);
            txtTest = (TextView) findViewById(R.id.txtTest2);


            // get the Intent that started this Activity
            Intent in = getIntent();

            // get the Bundle that stores the data of this Activity
            Bundle b = in.getExtras();

            // getting data from bundle
            conditions = b.getIntArray("condition");
            days = b.getStringArray("day");

            // show data to layout
            txtTest.setText("[Condition: " + conditions[0] + ", " + conditions[1] + ", " + conditions[2] + ", " + conditions[3] + "]");

    /*      for (int dc = 0; dc < conditions.length; dc++) {
                dayWeatherCondtion(dc, days[dc]);                   // 0 --- Today
                weatherConditionDress(dc, conditions[dc]);          // 0   10
            }*/
            btnPrevious.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }

            });

            btnNext.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Bundle b = new Bundle();

                    b.putInt("condition", conditions[1]);
                    b.putString("day", days[1]);

                    // Creating Intent object
                    Intent in = new Intent(SecondActivity.this, DetailActivity.class);

                    // Storing bundle object into intent
                    in.putExtras(b);
                    startActivity(in);
                }
            });
        }

        /*private void weatherConditionDress(int day, int condition) {   // 0 ----  10

            switch(condition){
            case 2: 
            {
                //String dress_array[] = ;
                weatherDress(day, getResources().getStringArray(R.array.five));      //0 ----- 
                break;
            }
            case 5:
            case 6:
            {
                //  String dress_array[] = getResources().getStringArray(R.array.five);
                weatherDress(day, getResources().getStringArray(R.array.five));
                break;
            }
            case 9:
            {
                //  String dress_array[] = getResources().getStringArray(R.array.five);
                weatherDress(day, getResources().getStringArray(R.array.nine));
                break;
            }
            case 11:
            {
                //weatherCondtion(day, getResources().getStringArray(R.string.condition_11));
                //String dress_array[] = getResources().getStringArray(R.array.five);
                //weatherDress(getResources().getStringArray(R.array.five));    
                break;
            }
            case 12:    
            {
                //String dress_array[] = getResources().getStringArray(R.array.five);
                weatherDress(day, getResources().getStringArray(R.array.five)); 
                break;
            }
            case 25:
            {
                //  String dress_array[] = getResources().getStringArray(R.array.five);
                weatherDress(day, getResources().getStringArray(R.array.twentyFive));
                break;
            }
            case 26:
            case 27:    
            case 28:
            case 29:
            case 30:
            case 31:
            {
                //  String dress_array[] = getResources().getStringArray(R.array.five);
                weatherDress(day, getResources().getStringArray(R.array.twentySix));
                break;  
            }
            case 36:
            {
                //  String dress_array[] = getResources().getStringArray(R.array.five);
                weatherDress(day, getResources().getStringArray(R.array.thirtySix));
                break;
            }
            case 37:
            case 38:
            case 39:
            case 44:    
            {
                //  String dress_array[] = getResources().getStringArray(R.array.five);
                weatherDress(day, getResources().getStringArray(R.array.thirtySeven));
                break;  
            }
            }
        }

        private void dayWeatherCondtion(int index, String day) {
            dayCell[index].setImageDrawable(getDrawableByName(this,day));
            Log.d("DAY","Day is " + day);
        }

        private void weatherDress(int day, String[] dress_array) {
            for(int d = 0; d  < dress_array.length; d++ ){
                //ImageView temp = dressImageView[d];
            Log.d("Dress Image", " "+ dressImageView[day][d]);  
            dressImageView[day][d].setImageDrawable(getDrawableByName(this, dress_array[d]));

            }
        }
        //
        public Drawable getDrawableByName(Context context, String name){
            Resources resources = context.getResources();
            final int resourceId = resources.getIdentifier(name, "drawable", 
                    context.getPackageName());
            return resources.getDrawable(resourceId);
        }
    */
    }