我无法弄清楚这一点

时间:2014-03-17 22:06:18

标签: android

我正在学习Android开发。运行下面的代码会导致出现此错误消息日志。我无法弄明白,是否有人看到我做错了什么?

03-17 23:02:59.387: E/Trace(15387): error opening trace file: No such file or directory (2)
03-17 23:02:59.387: D/ActivityThread(15387): setTargetHeapUtilization:0.25
03-17 23:02:59.397: D/ActivityThread(15387): setTargetHeapIdealFree:8388608
03-17 23:02:59.397: D/ActivityThread(15387): setTargetHeapConcurrentStart:2097152
03-17 23:02:59.497: D/AndroidRuntime(15387): Shutting down VM
03-17 23:02:59.497: W/dalvikvm(15387): threadid=1: thread exiting with uncaught exception (group=0x41bdc378)
03-17 23:02:59.507: E/AndroidRuntime(15387): FATAL EXCEPTION: main
03-17 23:02:59.507: E/AndroidRuntime(15387): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fb_ui_in_code/com.example.fb_ui_in_code.MainActivity}: java.lang.NullPointerException
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.os.Looper.loop(Looper.java:213)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.main(ActivityThread.java:4787)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at java.lang.reflect.Method.invokeNative(Native Method)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at java.lang.reflect.Method.invoke(Method.java:511)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at dalvik.system.NativeStart.main(Native Method)
03-17 23:02:59.507: E/AndroidRuntime(15387): Caused by: java.lang.NullPointerException
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3485)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.view.ViewGroup.addView(ViewGroup.java:3357)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.view.ViewGroup.addView(ViewGroup.java:3333)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:271)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.Activity.setContentView(Activity.java:1887)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.example.fb_ui_in_code.MainActivity.onCreate(MainActivity.java:21)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.Activity.performCreate(Activity.java:5008)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
03-17 23:02:59.507: E/AndroidRuntime(15387):    ... 11 more

代码

package com.example.fb_ui_in_code;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

    RelativeLayout main;
    EditText usernameInput, passwordInput;
    TextView message, username, password;
    Button login;
    LayoutParams dimensions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(main);

        init();
        createMessageTextView();
        main.addView(message, dimensions);

    }

    private void init() {
        // TODO Auto-generated method stub
        main = new RelativeLayout(this);
        LayoutParams mainDimensions = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        main.setLayoutParams(mainDimensions);

        usernameInput = new EditText(this);
        passwordInput = new EditText(this);
        message = new TextView(this);//please login first
        username = new TextView(this);
        password = new TextView(this);
        login = new Button(this);

    }
        private void createMessageTextView() {
            // TODO Auto-generated method stub

            LayoutParams dimensions=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            dimensions.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            message.setText("Please login first");
            message.setLayoutParams(dimensions);


        }
}

5 个答案:

答案 0 :(得分:2)

我相信setContentView(main)就是问题所在。您需要将其设置为RelativeLayout或使用setContentView(R.layout.main); main永远不会被初始化。

答案 1 :(得分:2)

赋予setContentView()的参数是整数资源ID。因此,在您的情况下,您需要使用ID“R.layout.main”等。我认为代码只能编译,因为你的'main'变量仍然是null并且被自动转换为int。

答案 2 :(得分:2)

你正在使用非常奇怪的方法。我不认为你真的需要从Java代码创建UI。通常,您必须在XML文件中创建布局。这是常见的方法。我的建议是不要浪费你的时间,至少在开始时。为什么?您可以阅读以下内容:Why using XML to create GUI is a good practice in Android

但无论如何,如果您需要在代码中创建用户界面,则必须在初始化setContentView(main)后调用main。只需将setContentView(main)放在onCreate()方法的末尾。

答案 3 :(得分:1)

好吧,似乎nullPointerExecption来自第21行,你有setContentView(main);

所以我可以谷歌'android developer setContentView'

而不是回答你

第一次参加时幸运 http://developer.android.com/reference/android/app/Activity.html

搜索setcontentview的页面并看到我需要传递一个引用我的内容视图的int,现在我问;是'主'初始化?我想用'主'做什么?请参阅,此活动中将使用的所有视图都将在此内容视图中引用feild。

我们正在研究的是建立与xml文件的接口,在本例中是一个布局。所以号码是R.layout.WHATEVER_YOU_NAMED_THE_LAYOUT_YOU_WANT_TO_USE

冷却? =]

答案 4 :(得分:0)

应该是这样的:

setContentView(R.layout.main);

并确保导入R,如:YOUR_PACKAGE_NAME.R