我正在使用Java创建一个Android Studio应用
我的问题是:
应用程式在开始说NullPointerException
时当机
为什么?
因为我试图在ListView上添加一个ontouch
事件,所以我可以
用户出于某种令人毛骨悚然的原因触摸的位置的X Y
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="List"
android:id="@+id/textView"
android:layout_centerHorizontal="true"
android:theme="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Title"
android:textSize="@dimen/abc_action_button_min_width_overflow_material"
android:textStyle="normal" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView2" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:longClickable="true"
android:layout_weight="0.65"
android:nestedScrollingEnabled="true">
</ListView>
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/imageButton"
android:layout_gravity="right"
android:background="@drawable/trash" />
</LinearLayout>
完整代码已更新
package com.example.discopc.yubismart;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
public class List1 extends ListActivity {
int x,y;
int lx,ly;
int width;
public ListView list;
public ListView list1x;
int clicks;
boolean isonthread;
//ImageButton addButton;
File file1;
File path1;
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;
//RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED
String Fname;
String Fip;
int Fport;
String Fcheck;
String Frele;
// int clickCounter=0;
// int n=0;
// int last;
int num=0;
// int flag1=0;
int[] first=new int[1000];
int[] end=new int[1000];
String line;
//String[] str = {"Lamp","Fridge","TV","Laptop","Charger","Switch","Door"};
char[]charline=new char[1000];
boolean connected;
String ip1;
String s_port1;
String name;
String rele;
String check;
int port1 = 0;
int ipflag = 0;
int portflag = 0;
int portnum = 0;
char[] charip1 = new char[100];
char[] charport1 = new char[100];
char[] charname =new char[100];
char[] charrele = new char[100];
char[] charcheck = new char[100];
int ipnum = 0;
int checknum = 0;
char[]chardata1 = new char[100];
String data1="";
int datanum;
int j = 0;
int num2;
int checkname=0;
int checkrele=0;
int nameflag = 0;
Socket socket;
int releflag = 0;
//int checkflag = 0;
int namenum = 0;
int relenum;
int posi=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
clicks = 0;
isonthread = false;
list = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
//listItems.add("New Item");
loadlist();
//This Is The Problem ! \/ \/ \/
list.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
int actionX = (int) event.getX();
int actionY = (int) event.getY();
int extraTapArea = 13;
x = (int) (actionX + extraTapArea);
y = (int) (actionY - extraTapArea);
return false;
}
});
//This Is The Problem ! /\ /\ /\
adapter.notifyDataSetChanged();
}
protected void onListItemClick(ListView l, View v, final int position, long id) {
clicks++;
posi=position;
lx=x;
ly=y;
Toast.makeText(List1.this, lx+" : "+ly,
Toast.LENGTH_SHORT).show();
if(!isonthread) {
isonthread=true;
new Thread(new Runnable() {
public void run() {
try {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
if ((x-lx)*(x-lx)+(y-ly)*(y-ly)<100) {
clicks = 0;
List1.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder adb2 = new AlertDialog.Builder(List1.this);
adb2.setTitle("Action :");
adb2.setMessage("Remove ? " + posi);
adb2.setNegativeButton("Remove", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
removeitem(posi);
}
});
adb2.setPositiveButton("Cancel", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
adb2.show();
}
});
isonthread=false;
} else {
clicks = 0;
List1.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder adb = new AlertDialog.Builder(List1.this);
adb.setTitle("Action :");
adb.setMessage("Connect " + posi);
final int positionToRemove = posi;
adb.setNegativeButton("Connect", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
posi = posi;
testconnection();
}
});
adb.setPositiveButton("Cancel", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
adb.show();
}
});
isonthread=false;
}
} catch (Exception e) {
}
}
}).start();
}
}
public void testconnection(){
try {
//isChecked=false;
for (int i = 0; i <= 99; i++) {
charport1[i]='\0';
charip1[i]='\0';
charname[i]='\0';
//charcheck[i]='\0';
charrele[i]='\0';
}
//connected=false;
num=0;
num2=0;
line=Read1();
charline=line.toCharArray();
for (int i=0;i<=charline.length-1;i++){
if (charline[i]=='*'){
first[num]=i;
num++;
}
if (charline[i]=='#') {
end[num2]=i;
num2++;
}
}
//super.onListItemClick(l, v, pos, id);
//Toast.makeText(this, str[position] + line, Toast.LENGTH_SHORT).show();
ipnum=0;
portnum=0;
datanum=0;
namenum=0;
relenum=0;
releflag=0;
for (int i = first[posi]; i <= end[posi]-1; i++) {
analyse(i);
}
//Toast.makeText(this, str[position] + datanum+"", Toast.LENGTH_SHORT).show();
j=0;
// Toast.makeText(this, str[position] + " S2FOR", Toast.LENGTH_SHORT).show();
//for (int i=checkname;i<=(checkrele-3);i++){
//charline[i]=charname[j];
//j++;
//}
convert_to_string_all();
ip1=ip1.replace(":","");
ip1=ip1.replaceAll("[^\\d.]","");
s_port1=s_port1.replaceAll("[\\D]","");
name=name.replace("$","");
//Toast.makeText(this, "-"+ip1+"-", LENGTH_SHORT).show();
Fname=name;
Fip=ip1;
Fcheck=check;
Fport=port1;
Frele=rele;
int portnew;
portnew = Integer.parseInt(s_port1);
port1= portnew;
//Toast.makeText(this, "-"+portnew+"-", LENGTH_SHORT).show();
j=0;
new Thread(new Runnable() {
public void run() {
try {
//###############################################################//
//###############################################################//
// ============== Connect ============== //
//###############################################################//
InetAddress serverAddr = InetAddress.getByName(ip1);
InetSocketAddress addz = new InetSocketAddress(ip1, port1);
socket = new Socket(serverAddr, port1);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
BufferedReader dis = new BufferedReader(new InputStreamReader(socket.getInputStream()));
connected=true;
//check = check.replace("no", "ok");
check="ok";
charcheck=check.toCharArray();
List1.this.runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(this, "wrong", Toast.LENGTH_SHORT).show();
//Toast.makeText(MainActivity.this, check, LENGTH_SHORT).show();
}
});
List1.this.runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(this, "wrong", Toast.LENGTH_SHORT).show();
//Toast.makeText(MainActivity.this, name, LENGTH_SHORT).show();
}
});
chardata1=data1.toCharArray();
j=0;
for (int i=checknum;i<=checkname-3;i++){
charline[i]=charcheck[j];
j++;
}
String fixline = new String(charline);
line = fixline;
Write1(line);
//###############################################################//
} catch (final Exception e) {
connected=false;
check="no";
charcheck=check.toCharArray();
List1.this.runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(MainActivity.this, check, LENGTH_SHORT).show();
}
});
List1.this.runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(MainActivity.this, name, LENGTH_SHORT).show();
}
});
chardata1=data1.toCharArray();
j=0;
for (int i=checknum;i<=checkname-3;i++){
charline[i]=charcheck[j];
j++;
}
String fixline = new String(charline);
line = fixline;
Write1(line);
List1.this.runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(this, "wrong", Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
//dos = new DataOutputStream(socket.getOutputStream());
//dis = new BufferedReader(new InputStreamReader(socket.getInputStream()));*/
}
catch (Exception e)
{
connected=false;
}
}
public void analyse(int i){
if (releflag==1)
{
charrele[relenum]=charline[i];
relenum++;
}
if (nameflag==1){
charname[namenum]=charline[i];
namenum++;
}
if (portflag == 1) {
charport1[portnum] = charline[i];
portnum++;
}
if (ipflag == 1) {
charip1[ipnum] = charline[i];
ipnum++;
}
if (charline[i]=='$'){
checkrele=i+1;
nameflag=0;
releflag=1;
}
if (charline[i]=='*'){
ipflag=1;
}
if (charline[i] == ':') {
ipflag = 0;
portflag = 1;
}
if (charline[i]=='^'){
nameflag=1;
checkname=i+1;
}
if (charline[i] == '@') {
checknum=i+1;
portflag=0;
}
if (charline[i] == '#') {
releflag=0;
}
chardata1[datanum]=charline[i];
datanum++;
}
public void removeitem(final int position){
final int positionToRemove = position;
listItems.remove(positionToRemove);
adapter.notifyDataSetChanged();
try{
for (int i = 0; i <= 99; i++) {
charport1[i]='\0';
charip1[i]='\0';
charname[i]='\0';
//charcheck[i]='\0';
charrele[i]='\0';
}
//connected=false;
num=0;
num2=0;
line=Read1();
charline=line.toCharArray();
for (int i=0;i<=charline.length-1;i++){
if (charline[i]=='*'){
first[num]=i;
num++;
}
if (charline[i]=='#') {
end[num2]=i;
num2++;
}
}
for (int i = first[position]; i <= end[position]; i++) {
charline[i]='\0';
}
String fixline = new String(charline);
line = fixline;
Write1(line);
}catch (Exception e){};
}
public void loadlist(){
try {
for (int i = 0; i <= 99; i++) {
charport1[i] = '\0';
charip1[i] = '\0';
charname[i] = '\0';
//charcheck[i]='\0';
charrele[i] = '\0';
}
//connected=false;
num = 0;
num2 = 0;
line = Read1();
charline = line.toCharArray();
for (int i = 0; i <= charline.length - 1; i++) {
if (charline[i] == '*') {
first[num] = i;
num++;
}
if (charline[i] == '#') {
end[num2] = i;
num2++;
}
}
//Toast.makeText(this, str[position] + line, Toast.LENGTH_SHORT).show();
ipnum = 0;
portnum = 0;
datanum = 0;
namenum = 0;
relenum = 0;
for (int i = 0; i <= 99; i++) {
charport1[i]='\0';
charip1[i]='\0';
charname[i]='\0';
//charcheck[i]='\0';
charrele[i]='\0';
}
for (int p = 0; p <= num-1; p++) {
ipnum = 0;
portnum = 0;
datanum = 0;
namenum = 0;
relenum=0;
releflag=0;
for (int i = 0; i <= 99; i++) {
charport1[i]='\0';
charip1[i]='\0';
charname[i]='\0';
//charcheck[i]='\0';
charrele[i]='\0';
}
for (int i = first[p]; i <= end[p]; i++) {
analyse(i);
}
j=0;
// Toast.makeText(this, str[position] + " S2FOR", Toast.LENGTH_SHORT).show();
/*for (int i=checkname;i<=(checkrele-3);i++){
charline[i]=charname[j];
j++;
}*/
convert_to_string_all();
ip1 = ip1.replace(":", "");
// s_port1=s_port1.replace("@","");
ip1 = ip1.replaceAll("[^\\d.]", "");
s_port1 = s_port1.replaceAll("[\\D]", "");
name=name.replace("$","");
listItems.add(name + "\n" + ip1 + " " + s_port1);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
}
}
public void convert_to_string_all(){
String fixrele = new String(charrele);
rele = fixrele;
String fixdata = new String(chardata1);
data1 = fixdata;
String fixline = new String(charline);
line = fixline;
String fixip = new String(charip1);
ip1 = fixip;
String fixport = new String(charport1);
s_port1 = fixport;
String fixname = new String(charname);
name = fixname;
}
private void Write1(String data) {
try {
Context context = getApplicationContext();
if(path1==null)path1 = context.getFilesDir();
file1 = new File(path1, "first.txt");
FileOutputStream stream = new FileOutputStream(file1);
try {
stream.write(data.getBytes());
} finally {
stream.close();
}
}
catch (IOException e) {
}
}
private String Read1() throws IOException {
String ret = "";
Context context = getApplicationContext();
if(path1==null)path1 = context.getFilesDir();
file1 = new File(context.getFilesDir(), "first.txt");
int length = (int) file1.length();
byte[] bytes = new byte[length];
FileInputStream in = new FileInputStream(file1);
try {
in.read(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
in.close();
}
String contents = new String(bytes);
ret=contents;
return ret;
}
}
更新的代码可能令人毛骨悚然:D
我想要的是 ?
我想在列表视图中保持联系
对不起,英语不好!
logcat(我的didnet知道可以准确地复制什么,而我无法复制所有内容,因为它扩展了主体的最大字符)
07-18 14:17:06.010 7805-7805 / com.example.discopc.yubismart W / dalvikvm:VFY:无法解析虚拟方法402:Landroid / content / res / TypedArray; .getType(I)I
07-18 14:17:18.680 7805-7805 / com.example.discopc.yubismart W / dalvikvm:threadid = 1:线程以未捕获的异常退出(group = 0x41660c50)
最佳答案
在您的代码调用中:setContentView(R.layout.activity_list);
首先,否则listview不能从Xml中找到引用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
clicks=0;
isonthread=false;
list = (ListView) findViewById(R.id.list);
编辑:
在活动中更改您的
ListView
参考: list = (ListView) findViewById(android.R.id.list);
使用时:
android:id="@android:id/list"