我试图弹出一个表单,但它一直在崩溃。我的错误在哪里?
这是mainactivity.java:
package com.javacodegeeks.android.fragmentstest;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
@SuppressLint("InflateParams")
public class MainActivity extends Activity implements OnClickListener {
ImageView mButton1;
Context contex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1 = (ImageView) findViewById(R.id.button1);
mButton1.setOnClickListener(new OnClickListener() {
@SuppressWarnings("null")
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupform, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDbutton1 = (Button)popupView.findViewById(R.id.button1);
View btnbutton1 = null;
btnbutton1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
// View btnbutton1 = null;
popupWindow.showAsDropDown(btnbutton1, 50, -30);
}
});
}
请注意,此后还有更多内容,但它与我遇到问题的部分无关。
这是popupform.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"
android:background="#ccffcc"
android:orientation="vertical" >
<EditText
android:id="@+id/editText10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="132dp"
android:ellipsize="start"
android:ems="12"
android:hint="UserName"
android:textColor="#00cd00"
android:textColorHint="#00cd00"
android:textStyle="normal" />
<EditText
android:id="@+id/editText11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText10"
android:layout_centerHorizontal="true"
android:ellipsize="start"
android:ems="12"
android:hint="Password"
android:textColor="#00cd00"
android:textColorHint="#00cd00"
android:textStyle="normal" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="26dp"
android:text="LOGIN HERE"
android:textStyle="bold" />
</RelativeLayout>
这是activity_main.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"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:background="#82c346" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:scaleType="fitStart"
android:src="@drawable/logo" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/actionBarBottom"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
android:background="#d04051" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<ImageView
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:onClick="selectFrag"
android:scaleType="centerInside"
android:src="@drawable/sign_in" >
</ImageView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/actionBarTop"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/logo"
android:layout_weight="1"
android:background="#8a40d0" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<ImageView
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="selectFrag"
android:scaleType="center"
android:src="@drawable/menu_scan" />
<ImageView
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="selectFrag"
android:scaleType="center"
android:src="@drawable/menu_menu" />
<ImageView
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="selectFrag"
android:scaleType="center"
android:src="@drawable/menu_cart" />
</LinearLayout>
</RelativeLayout>
<fragment
android:id="@+id/fragment"
android:name="com.javacodegeeks.android.fragmentstest.Fragment3"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_above="@id/actionBarBottom"
android:layout_below="@id/actionBarTop"
android:layout_centerInParent="true" />
</RelativeLayout>
答案 0 :(得分:1)
在popView中,你没有按钮1导致
Button btnDbutton1 = (Button)popupView.findViewById(R.id.button1);
返回null。弹出窗口xml中按钮的id为R.id.login
,因此它应该是
Button btnDbutton1 = (Button)popupView.findViewById(R.id.login);
另外,摆脱
View btnbutton1 = null;
btnbutton1.setOnClickListener
由@PedroOlivera指出
答案 1 :(得分:0)
由于这一行而崩溃:
Button btnDbutton1 = (Button)popupView.findViewById(R.id.button1);
您的popupform.XML
没有R.id.button1
按钮。
将以上行替换为以下行:
Button btnDbutton1 = (Button)popupView.findViewById(R.id.login);