gpt4 book ai didi

java - Android Java - view1.setAlpha 处出现 NullpointerException?

转载 作者:行者123 更新时间:2023-12-01 10:07:34 25 4
gpt4 key购买 nike

首先:抱歉代码很长。如果您能看一下,我将非常高兴...更多详细信息在底部。

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;
}
}

我的 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>

这是我的代码。在第 78 行(在 Android Studio 中 view1.setalpha 处的 public void crossfade),它给了我一个空指针异常,我不明白为什么。请帮我!!我没有更改 gradle 文件等...

最佳答案

转到您的 onCreate 方法并替换它

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

有了这个

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

因为您在方法中定义了一个新变量,所以全局变量保持为 null。

关于java - Android Java - view1.setAlpha 处出现 NullpointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36339239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com