I've searched for other similiar questions, and i've found some, but strangely they don't work for me, and i don't know what i'm doing wrong...
public class MainActivity extends Activity {
EditText item;
Button insert;
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
item = (EditText) findViewById(R.id.item);
layout = (RelativeLayout) findViewById(R.id.layout);
}
@Override
public void onResume() {
super.onResume();
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s_item = item.getText().toString();
TextView tv = new TextView(MainActivity.this);
layout.addView(tv);
tv.setText(s_item);
}
});
}
}
when the app starts, it just crashes... and the problem is in this code apparently...
LOGCAT
08-18 14:37:19.425 2981-2981/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {g.d.raven/g.d.raven.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at g.d.raven.MainActivity.onResume(MainActivity.java:30)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
at android.app.Activity.performResume(Activity.java:3832)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
... 12 more
08-18 14:37:19.429 130-143/? E/: Dumpstate > /data/log/dumpstate_app_error
Thank you all, i found it by myself just now, forgot to instantiate the button XD
try this:
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView tv;
String s_item = item.getText().toString();
tv = new TextView(MainActivity.this);
layout.addView(tv);
tv.setText(s_item);
}
});
First u need to initilaze Button insert :
Try this :
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText item;
Button insert;
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
item = (EditText) findViewById(R.id.editText1);
layout = (RelativeLayout) findViewById(R.id.relative);
insert = (Button) findViewById(R.id.button1);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button b = (Button) view;
String s_item = b.getText().toString();
TextView tv = new TextView(MainActivity.this);
layout.addView(tv);
tv.setText(s_item);
}
});
}
@Override
public void onResume() {
super.onResume();
}
}
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
All credit to sechastain, to relatively position your items programmatically you have to assign ids to them.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.