这里是下面的logcat,我无法理解空指针exeption的含义,或者它下面的任何东西。
08-18 14:59:21.080: E/AndroidRuntime(13333): FATAL EXCEPTION: main
08-18 14:59:21.080: E/AndroidRuntime(13333): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{k.t.m/k.t.m.MainActivity}: java.lang.NullPointerException
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.app.ActivityThread.access$600(ActivityThread.java:128)
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.os.Looper.loop(Looper.java:137)
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.app.ActivityThread.main(ActivityThread.java:4514)
08-18 14:59:21.080: E/AndroidRuntime(13333): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 14:59:21.080: E/AndroidRuntime(13333): at java.lang.reflect.Method.invoke(Method.java:511)
08-18 14:59:21.080: E/AndroidRuntime(13333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
08-18 14:59:21.080: E/AndroidRuntime(13333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
08-18 14:59:21.080: E/AndroidRuntime(13333): at dalvik.system.NativeStart.main(Native Method)
08-18 14:59:21.080: E/AndroidRuntime(13333): Caused by: java.lang.NullPointerException
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.app.Activity.findViewById(Activity.java:1794)
08-18 14:59:21.080: E/AndroidRuntime(13333): at k.t.m.MainActivity.<init>(MainActivity.java:14)
08-18 14:59:21.080: E/AndroidRuntime(13333): at java.lang.Class.newInstanceImpl(Native Method)
08-18 14:59:21.080: E/AndroidRuntime(13333): at java.lang.Class.newInstance(Class.java:1319)
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
08-18 14:59:21.080: E/AndroidRuntime(13333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
08-18 14:59:21.080: E/AndroidRuntime(13333): ... 11 more
这是我到目前为止的主要java ...感谢所有帮助我解决这个问题的人。
package k.t.m;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.support.v4.app.NavUtils;
import android.widget.*;
public class MainActivity extends Activity {
TextView main = (TextView) findViewById(R.id.main);
Button mainButton1 = (Button) findViewById(R.id.mainButton1);
Button mainButton2 = (Button) findViewById(R.id.mainButton2);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
milesToKilometers();
}
});
mainButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
kilometersToMiles();
}
});
}
protected void kilometersToMiles() {
main.setVisibility(View.INVISIBLE);
mainButton1.setVisibility(View.INVISIBLE);
mainButton2.setVisibility(View.INVISIBLE);
}
protected void milesToKilometers() {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:4)
在致电findViewById()
之前,您正在致电setContentView()
,但效果不佳。