0

我搜索了其他类似的问题,我找到了一些,但奇怪的是它们对我不起作用,我不知道我做错了什么......

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

4

3 回答 3

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());

于 2013-08-18T12:58:35.723 回答
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);
   }
});
于 2013-08-18T12:42:47.550 回答
0

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();

    }
}
于 2013-08-18T12:54:50.937 回答