This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
4年前关闭。
首先:对不起,LONG代码。如果您可以看一下,我将非常高兴。
我的Activity_Main.xml
这是我的代码。在第78行(在Android Studio中,在view1.setalpha的公共无效交叉淡入淡出处),它给了我一个nullpointerexception,我不知道为什么。
请帮我!!我没有更改gradle文件等...
(12个答案)
4年前关闭。
首先:对不起,LONG代码。如果您可以看一下,我将非常高兴。
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的公共无效交叉淡入淡出处),它给了我一个nullpointerexception,我不知道为什么。
请帮我!!我没有更改gradle文件等...
最佳答案
转到您的onCreate方法并将其替换View HomeView = (View) findViewById(R.id.HomeView); View ChatView = (View) findViewById(R.id.ChatView);
有了这个HomeView = (View) findViewById(R.id.HomeView); ChatView = (View) findViewById(R.id.ChatView);
因为您要在方法中定义新变量,所以全局变量保持为空。
关于java - Android Java-NullpointerException在view1.setAlpha吗? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36339239/
10-14 07:25