gsf*_*sfd 2 java android nullpointerexception
这是我的主要课程:
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
public final class HomeScreen implements OnTouchListener
{
public HomeScreen(Viewer view)
{
this.view = view;
RelativeLayout layout = (RelativeLayout)view.findViewById(R.id.home_screen_lay);
layout.setOnTouchListener(this); // this is where the error points to
network.setFocus(this);
view.setContentView(R.layout.home);
}
public boolean onTouch(View v, MotionEvent e)
{
switch(e.getAction())
{
case MotionEvent.ACTION_DOWN:
onPress(v,e);
break;
case MotionEvent.ACTION_MOVE:
onMove(v,e);
break;
case MotionEvent.ACTION_UP:
onRelease(v,e);
break;
}
return true;
}
public void onPress(View v, MotionEvent e)
{
for(int x=0;x<buttons.length;x++)
{
if(buttons[x].contains((int)e.getX(),(int)e.getY()))
{
if(buttons[x]==exitRect)
view.finish();
if(buttons[x]==newUserRect)
new NewUser(view,network);
if(buttons[x]==loginRect)
new Login(view,network);
}
}
}
public void onMove(View v, MotionEvent e){}
public void onRelease(View v, MotionEvent e){}
public void handleMessage(String message){}
private NetworkManager network;
private Viewer view;
private Rect loginRect;
private Rect newUserRect;
private Rect exitRect;
private Rect[] buttons;
}
Run Code Online (Sandbox Code Playgroud)
和继承人home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_screen_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/home_screen">
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
NullPointerExeption指向我的HomeScreen类中的那一行,它表示布局为null.我已经看了好几个小时了,我看不出什么是错的.别人能看到我错过的东西吗?欢迎所有相关答案!
如果你搬家怎么办:
view.setContentView(R.layout.home);
Run Code Online (Sandbox Code Playgroud)
在尝试获取布局对象之前到方法的顶部?