2013-06-07 4 views
2

привет, это, возможно, было задано раньше, но я не мог найти ничего конкретного. когда я иду запускать свою программу, я придумываю исключение nullpointer и программа не запускается. им все еще любопытно это и не знаю, где моя проблема.Почему я продолжаю получать nullpointerexception?

public class MainActivity extends Activity implements OnClickListener { 
public ArrayList<Short> indexP = new ArrayList<Short>(); 
public ArrayList<Float> linep = new ArrayList<Float>(); 
public Float coords = (float) 0; 
public short p = 0; 
public TextView info = (TextView) findViewById(R.id.info); 
public int l = 0; 
GLSurfaceView ourSurface; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.cad); 
    ourSurface = new GLSurfaceView(this); 
    FrameLayout v = (FrameLayout) findViewById(R.id.display); 
    v.addView(ourSurface); 
    ourSurface.setRenderer(new GLRenderer()); 

    Button line = (Button) findViewById(R.id.line); 
    final Button enter = (Button) findViewById(R.id.enter); 
    EditText cl = (EditText) findViewById(R.id.cl); 
    final String value = cl.getText().toString(); 
    try { 
     coords = Float.parseFloat(value); 
    } catch (NumberFormatException e) { 
    } 
    ; 

    line.setOnClickListener(this); 
    enter.setOnClickListener(this); 
    enter.setOnClickListener(this); 
} 

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.line: 
     info.setText("Input X"); 
     break; 
    case R.id.enter: 
     switch (l) { 
     case (0): 
      linep.add(coords); 
      l++; 
      break; 
     case (1): 
      linep.add(coords); 
      indexP.add(p); 
      l = 0; 
      p++; 

     } 
    } 

} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
} 

}

и Херес Информация LogCat:

threadid=1: thread exiting with uncaught exception (group=0x40a961f8) 
FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.linecad/com.example.linecad.MainActivity}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1991) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092) 
    at android.app.ActivityThread.access$600(ActivityThread.java:126) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1172) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4586) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
    at android.app.Activity.findViewById(Activity.java:1840) 
    at com.example.linecad.MainActivity.<init>(MainActivity.java:20) 
    at java.lang.Class.newInstanceImpl(Native Method) 
    at java.lang.Class.newInstance(Class.java:1319) 
    at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1982) 
    ... 11 more 

ответ

7

в этом:

public TextView info = (TextView) findViewById(R.id.info); //<<<< 

вы пытаетесь получить доступ Просмотров от текущей деятельности до установки макета для него. поэтому переместите все виды инициализации в методе после вызова setContentView as:

public TextView info; //<<< declare here... 
public int l = 0; 
GLSurfaceView ourSurface; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.cad); 
    info = (TextView) findViewById(R.id.info); //<<< initialize here... 
    ...... 
Смежные вопросы