0

First: Sorry for the LONG code. I would be very happy if you could take a look at it... More details at the bottom.

package com.as.zippedmessagingservice;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.crypto.Cipher;

import static com.as.zippedmessagingservice.MainActivity.getHashElement;
import static com.as.zippedmessagingservice.MainActivity.getHashKeys;









public class MainActivity extends AppCompatActivity {
    private int ShortAnimationDuration;
    private View HomeView;
    private View ChatView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //! Get UI Items
        TextView frame_title = (TextView) findViewById(R.id.frame_title);
        ListView contacts = (ListView) findViewById(R.id.listview);
        View HomeView = (View) findViewById(R.id.HomeView);                     //Get Home View from activity_main.xml
        View ChatView = (View) findViewById(R.id.ChatView);                     //Get Chat View from activity_main.xml
        ChatView.setVisibility(View.GONE);                                            //Hide the Chat screen
        frame_title.setText(R.string.recent_convs);                                   // Set Frame Title
        //! Create Variables and other Stuff
        ShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
        LinkedHashMap<String,String> contact_tree = new LinkedHashMap<String,String>() {{
                                                                put("Android","Hello Guys ;-)");
                                                                put("iPhone","What's up Folks?");
                                                                put("Linux","Hi ;D");  }};


        final MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, contact_tree);
        contacts.setAdapter(adapter);

        contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    int position, long id) {
                final String item = (String) parent.getItemAtPosition(position);
                goToChat(item);

                }
        });
    }
    public static String[] getHashElement(HashMap<String,String> hash, Integer item_index) {
        for (Map.Entry entry : hash.entrySet()) {
            if (item_index-- == 0) {
                return new String[] {(String) entry.getKey(),(String) entry.getValue()}; }
        }
        return new String[] {"getHashElement(): Requested Item Index not in Range","Error! See getHashElement()[0] for more details..."};
    }
    public static List getHashKeys(HashMap<String,String> hash) { List<String> indexes = new ArrayList<String>(hash.keySet()); return indexes;}
    private void goToChat(String item) {
        crossfade(ChatView, HomeView);
        //getActionBar().setTitle(item);
    }
    private void crossfade(final View view1, final View view2) {
        // Set the content view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        view1.setAlpha(0f);
        view1.setVisibility(View.VISIBLE);

        // Animate the content view to 100% opacity, and clear any animation
        // listener set on the view.
        view1.animate()
                .alpha(1f)
                .setDuration(ShortAnimationDuration)
                .setListener(null);

        // Animate the loading view to 0% opacity. After the animation ends,
        // set its visibility to GONE as an optimization step (it won't
        // participate in layout passes, etc.)
        view2.animate()
                .alpha(0f)
                .setDuration(ShortAnimationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        view2.setVisibility(View.GONE);
                    }
                });
    }
}


class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final HashMap<String, String> values;
    public MySimpleArrayAdapter(Context context, LinkedHashMap<String,String> values) {
        super(context, -1, getHashKeys(values));
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.single_contact_layout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.firstLine);
        TextView lastMessage = (TextView) rowView.findViewById(R.id.secondLine);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(getHashElement(values,position)[0]);
        lastMessage.setText(getHashElement(values,position)[1]);
        // change the icon for Windows and iPhone
        String s = getHashElement(values,position)[0];
        if (s.startsWith("iPhone")) {
            imageView.setImageResource(R.mipmap.ic_launcher);
        } else {
            imageView.setImageResource(R.drawable.contact);
        }

        return rowView;
    }
}

My Activity_Main.xml

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.as.zippedmessagingservice.MainActivity"
        android:id="@+id/HomeView">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="{frame_title}"
            android:textSize="20dp"
            android:id="@+id/frame_title" />
        <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/listview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.as.zippedmessagingservice.MainActivity"
        android:id="@+id/ChatView">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/chat_here_later"
            android:textSize="40dp"
            android:id="@+id/chat_here_later" />
    </LinearLayout>
</FrameLayout>

This is my code. At Line 78 (in Android Studio at public void crossfade at view1.setalpha) it gives me a nullpointerexception and i can't figure out why. Please help me!! I didn't change the gradle files etc...

famemaker
  • 111
  • 7

1 Answers1

1

Go to your onCreate method and replace this

View HomeView = (View) findViewById(R.id.HomeView); View ChatView = (View) findViewById(R.id.ChatView);

with this

HomeView = (View) findViewById(R.id.HomeView); ChatView = (View) findViewById(R.id.ChatView);

Because you're defining a new variable in your method the global variable remains null.

Axel
  • 568
  • 7
  • 18