我搜索过其他类似的问题,我找到了一些,但奇怪的是它们不适合我,我不知道我做错了什么......
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);
}
});
}
}
当应用程序启动时,它只会崩溃......问题显然在这个代码中......
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
谢谢大家,我刚才自己找到了,忘了实例化按钮XD
答案 0 :(得分:0)
试试这个:
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);
}
});
答案 1 :(得分:0)
首先你需要初始化
试试这个:
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();
}
}
答案 2 :(得分:0)
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);
所有归功于sechastain,为了以编程方式相对定位您的项目,您必须为其分配ID。
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
然后addRule(RelativeLayout.RIGHT_OF,tv1.getId());