package com.ex06.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityButton extends Activity {
Button button1 = (Button) findViewById (R.id.button1);
Button button2 = (Button) findViewById (R.id.button2);
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
button1.setOnClickListener (new View.OnClickListener () {
public void onClick (View v) {
setTitle ("button1 clicked invisible");
button1.setVisibility (View.INVISIBLE);
button2.setVisibility (View.VISIBLE);
}
});
button2.setOnClickListener (new View.OnClickListener () {
public void onClick (View v) {
setTitle ("button2 clicked invisible");
button2.setVisibility (View.INVISIBLE);
button1.setVisibility (View.VISIBLE);
}
});
}
}
I built two button, why this happens
logcat
12-02 20:57:40.865: E / AndroidRuntime (1658): FATAL EXCEPTION: main
12-02 20:57:40.865: E / AndroidRuntime (1658): Process: com.ex06.button, PID: 1658
12-02 20:57:40.865: E / AndroidRuntime (1658): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {com.ex06.button/com.ex06.button.ActivityButton}: java.lang.NullPointerException
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2102)
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2226)
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.app.ActivityThread.access $ 700 (ActivityThread.java: 135)
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 1397)
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.os.Handler.dispatchMessage (Handler.java: 102)
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.os.Looper.loop (Looper.java: 137)
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.app.ActivityThread.main (ActivityThread.java: 4998)
12-02 20:57:40.865: E / AndroidRuntime (1658): at java.lang.reflect.Method.invokeNative (Native Method)
12-02 20:57:40.865: E / AndroidRuntime (1658): at java.lang.reflect.Method.invoke (Method.java: 515)
12-02 20:57:40.865: E / AndroidRuntime (1658): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 777)
12-02 20:57:40.865: E / AndroidRuntime (1658): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 593)
12-02 20:57:40.865: E / AndroidRuntime (1658): at dalvik.system.NativeStart.main (Native Method)
12-02 20:57:40.865: E / AndroidRuntime (1658): Caused by: java.lang.NullPointerException
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.app.Activity.findViewById (Activity.java: 1883)
12-02 20:57:40.865: E / AndroidRuntime (1658): at com.ex06.button.ActivityButton. (ActivityButton.java: 11)
12-02 20:57:40.865: E / AndroidRuntime (1658): at java.lang.Class.newInstanceImpl (Native Method)
12-02 20:57:40.865: E / AndroidRuntime (1658): at java.lang.Class.newInstance (Class.java: 1208)
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.app.Instrumentation.newActivity (Instrumentation.java: 1061)
12-02 20:57:40.865: E / AndroidRuntime (1658): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2093)
12-02 20:57:40.865: E / AndroidRuntime (1658): ... 11 more
12-02 20:57:45.135: I / Process (1658): Sending signal PID:. 1658 SIG: 9
Reply:
Button button1 = (Button) findViewById (R.id.button1);
Button button2 = (Button) findViewById (R.id.button2);
These two should be placed after oncreate of setcontent
Reply:
Upstairs positive solution!
setContentView (before), findViewById () will be the exception. Because View is not initialized.
Reply:
How to put the setcontent? Novice neighborhoods
Reply:
When button1 and button2 to him that can not be defined initialization value should be assigned after setContentView.
Reply:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.ex06.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityButton extends Activity {
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.XXXX); / / Here is the name of your layout file. In res \ layout below.
Button button1 = (Button) findViewById (R.id.button1);
Button button2 = (Button) findViewById (R.id.button2);
button1.setOnClickListener (new View.OnClickListener () {
public void onClick (View v) {
setTitle ("button1 clicked invisible");
button1.setVisibility (View.INVISIBLE);
button2.setVisibility (View.VISIBLE);
}
});
button2.setOnClickListener (new View.OnClickListener () {
public void onClick (View v) {
setTitle ("button2 clicked invisible");
button2.setVisibility (View.INVISIBLE);
button1.setVisibility (View.VISIBLE);
}
});
}
}
So you do not understand, I suggest you go to see the Android ready Samples projects.
Reply:
To learn to see log ah, ah still find a few examples to write about, I feel you do not have entry-oh
Reply:
2nd Floor positive solution, see more sample landlord
Reply:
Ah just read the book a few days, anxious to use, not previously exposed. Could you tell a little look at log predecessors experiences or methods? Thank you, first of ~ ~
Reply:
Find the sample are feeling no comments, looks good sweaty, seniors are some good examples, seeking ~ ~ ~

Reply:
android sdk comes with several sample is not for you, there is more to see http://developer.android.com/guide/components/index.html things right here should be helpful
Reply:
Landlord setContentView () Where? ? ? ? ? Actually not an error?
No comments:
Post a Comment