我有这个java类:
package com.app;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class Ajax extends Activity{
TextView TVajax;
Context context;
Button hint1,hint2,check,used,right;
EditText answer;
int points=1;
final int ALERT_DIALOG=1;
PopupWindow popUp;
TextView title,message;
Button close,stats;
public static boolean right(String[] a,String b)
//in=String array and string
//out=if the string is equal/close to one of the strings in the array-true else-false
{
boolean flag=false;
for(int i=0;i<a.length;i++)
{
int count=0;
if(a[i].length()==b.length()){
if(a[i].equalsIgnoreCase(b))
return true;
else{if(a[i].length()>6){
for(int j=0;j<a[i].length();j++)
{
if(a[i].charAt(j)==b.charAt(j))
count++;
}
if(count==a[i].length()-1)
flag=true;
}
}
}
}
return flag;
}
public void messageBox(String[] a,String b)
//in=get the answer of the "right" method and the present name
//out=if true-change the "TVajax" to the present name and pop up a message; false-nothing
{
Context context=getApplicationContext();
boolean f=false;
if(!right(a,b)){
if(b.length()<=6){
for(int i=0;i<a.length&&!f;i++)
{
int fl=-1;
int count=0;
int diff=a[i].length()-b.length();
switch(diff){
case 0:
for(int j=0;j<a[i].length();j++)
{
if(a[i].charAt(j)==b.charAt(j))
count++;
if(count==a[i].length()-1){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
}
break;
case 1:
if(a[i].startsWith(b)||a[i].endsWith(b)){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
break;
case -1:
if(b.startsWith(a[i])||b.endsWith(a[i])){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
else{
for(int j=1;j<a[i].length()&&fl==-1;j++)
{
if(a[i].charAt(j)!=b.charAt(j)&&a[i].charAt(j)==b.charAt(j+1)){
fl=j;
if(a[i].substring(0, j).equalsIgnoreCase(b.substring(0, j))&&a[i].substring(j).equalsIgnoreCase(b.substring(j+1))){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
}
}
}
default:;
}
}
}else{
for(int i=0;i<a.length&&!f;i++)
{
int fm=-1,sm=-1;
int diff=a[i].length()-b.length();
int count=0;
switch(diff){
case 0:
for(int j=0;j<a[i].length();j++)
{
if(a[i].charAt(j)==b.charAt(j))
count++;
if(count==a[i].length()-2){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
}
break;
case 1:
if(b.startsWith(a[i])||b.endsWith(a[i])){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
break;
case 2:
if(a[i].indexOf(b)!=-1){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
break;
case -1:
for(int j=0;j<a[i].length()&&(fm==-1||sm==-1);j++)
{
if(a[i].charAt(j)!=b.charAt(j)&&fm==-1)
fm=j;
if(a[i].charAt(j)!=b.charAt(j)&&fm!=-1)
sm=j;
}
if(b.substring(0,fm).equalsIgnoreCase(a[i].substring(0, fm))&&b.substring(fm+1, sm).equalsIgnoreCase(a[i].substring(fm, sm))&&b.substring(sm+1).equalsIgnoreCase(a[i].substring(sm))){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
break;
case -2:
if(b.indexOf(a[i])!=-1){
f=true;
CharSequence text = "Almost perfect,you'r close!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
break;
}
}
}
if(!f){
CharSequence text = "wrong answer,try again!";
int duration = Toast.LENGTH_SHORT;
Toast popuptoast=Toast.makeText(context, text, duration);
popuptoast.show();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ajax);
initialize();
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String show="Amsterdamsche Football Club Ajax NV";
String[] answers={"amsterdamsche football club ajax nv","ajax","afc","afc ajax","afc ajax amsterdam","amsterdam","ajax amsterdam","ajax afc","ajax amsterdam afc","afc amsterdam","amsterdam ajax afc","amsterdam ajax"};
answer = (EditText) findViewById(R.id.ETajax);
String answera=answer.getText().toString();
if(right(answers,answera)){
TVajax.setText(show);
TVajax.setTextColor(Color.RED);
popUp.showAtLocation(findViewById(R.layout.popup), Gravity.BOTTOM, 10, 10);
popUp.setWidth(170);
popUp.setHeight(70);
title.setText("Perfect!");
title.setTextColor(Color.GREEN);
message.setText("points: "+points);
message.setTextColor(Color.DKGRAY);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
stats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent statss=new Intent();
startActivity(statss);
}
});
}
else{
messageBox(answers,answera);
}
}
});
}
private void initialize() {
// TODO Auto-generated method stub
TVajax= (TextView) findViewById(R.id.TVajax);
hint1 = (Button) findViewById(R.id.Hint1);
hint2 = (Button) findViewById(R.id.Hint2);
check = (Button) findViewById(R.id.Bcheck);
right = (Button) findViewById(R.id.BshowLetters);
used = (Button) findViewById(R.id.BusedLetters);
stats = new Button(this);
stats.findViewById(R.id.bStattistics);
close = new Button(this);
close.findViewById(R.id.bClose);
title = new TextView(this);
title.findViewById(R.id.tvTitle);
message = new TextView(this);
message.findViewById(R.id.tvMessage);
popUp =new PopupWindow(this);
}
这个popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bStattistics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="180dp"
android:text="Stats" />
<Button
android:id="@+id/bClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/bStattistics"
android:layout_alignBottom="@+id/bStattistics"
android:layout_alignRight="@+id/tvMessage"
android:layout_marginRight="17dp"
android:text="Close" />
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle"
android:layout_marginRight="20dp"
android:layout_marginTop="31dp"
android:layout_toLeftOf="@+id/bStattistics"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="30dp" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/tvMessage"
android:layout_marginTop="28dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40dp" />
</RelativeLayout>
我正在尝试执行 PopupWindow 并将弹出内容视图设置为 R.layout.popup (popup.xml)并且没有显示的窗口
这是日志:
06-30 10:48:06.410: E/AndroidRuntime(1856): FATAL EXCEPTION: main
06-30 10:48:06.410: E/AndroidRuntime(1856): java.lang.NullPointerException
06-30 10:48:06.410: E/AndroidRuntime(1856): at com.app.Ajax$1.onClick(Ajax.java:222)
06-30 10:48:06.410: E/AndroidRuntime(1856): at android.view.View.performClick(View.java:2485)
06-30 10:48:06.410: E/AndroidRuntime(1856): at android.view.View$PerformClick.run(View.java:9080)
06-30 10:48:06.410: E/AndroidRuntime(1856): at android.os.Handler.handleCallback(Handler.java:587)
06-30 10:48:06.410: E/AndroidRuntime(1856): at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 10:48:06.410: E/AndroidRuntime(1856): at android.os.Looper.loop(Looper.java:123)
06-30 10:48:06.410: E/AndroidRuntime(1856): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-30 10:48:06.410: E/AndroidRuntime(1856): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 10:48:06.410: E/AndroidRuntime(1856): at java.lang.reflect.Method.invoke(Method.java:507)
06-30 10:48:06.410: E/AndroidRuntime(1856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-30 10:48:06.410: E/AndroidRuntime(1856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-30 10:48:06.410: E/AndroidRuntime(1856): at dalvik.system.NativeStart.main(Native Method)
请帮我解决这个问题
非常感谢!