Android newb在这里。编写我的第一个hello world应用程序并且无法通过两个“不幸”错误和崩溃。我猜它是一个空指针异常,但没有任何谷歌搜索让我更好地理解它。非常感谢你。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eventexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.eventexample.EventExampleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EventExampleActivity.java
package com.example.eventexample;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.TextView;
public class EventExampleActivity extends Activity {
@Override
protected void onStart()
{
super.onStart();
Button button =
(Button)findViewById(R.id.myButton);
button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText("Button clicked");
}
}
);
}
}
activity_event_example.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".EventExampleActivity" >
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/mybutton_string" />
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/myButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp"
android:text="@string/mytextview_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
控制台转储
[2014-07-06 21:35:36 - EventExample] ------------------------------
[2014-07-06 21:35:36 - EventExample] Android Launch!
[2014-07-06 21:35:36 - EventExample] adb is running normally.
[2014-07-06 21:35:36 - EventExample] Performing com.example.eventexample.EventExampleActivity activity launch
[2014-07-06 21:35:38 - EventExample] Application already deployed. No need to reinstall.
[2014-07-06 21:35:38 - EventExample] Starting activity com.example.eventexample.EventExampleActivity on device 192.168.56.101:5555
[2014-07-06 21:35:38 - EventExample] ActivityManager: WARNING: linker: libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix.
[2014-07-06 21:35:39 - EventExample] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.eventexample/.EventExampleActivity }
LogCat转储
07-07 02:35:38.276: E/AndroidRuntime(1681): FATAL EXCEPTION: main
07-07 02:35:38.276: E/AndroidRuntime(1681): Process: com.example.eventexample, PID: 1681
07-07 02:35:38.276: E/AndroidRuntime(1681): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eventexample/com.example.eventexample.EventExampleActivity}: java.lang.NullPointerException
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.os.Handler.dispatchMessage(Handler.java:102)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.os.Looper.loop(Looper.java:136)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-07 02:35:38.276: E/AndroidRuntime(1681): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 02:35:38.276: E/AndroidRuntime(1681): at java.lang.reflect.Method.invoke(Method.java:515)
07-07 02:35:38.276: E/AndroidRuntime(1681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-07 02:35:38.276: E/AndroidRuntime(1681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-07 02:35:38.276: E/AndroidRuntime(1681): at dalvik.system.NativeStart.main(Native Method)
07-07 02:35:38.276: E/AndroidRuntime(1681): Caused by: java.lang.NullPointerException
07-07 02:35:38.276: E/AndroidRuntime(1681): at com.example.eventexample.EventExampleActivity.onStart(EventExampleActivity.java:25)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.app.Activity.performStart(Activity.java:5241)
07-07 02:35:38.276: E/AndroidRuntime(1681): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
07-07 02:35:38.276: E/AndroidRuntime(1681): ... 11 more
答案 0 :(得分:0)
您必须让活动知道要为UI呈现哪个xml。因此,在您的活动中覆盖onCreate方法并添加此调用:setContentView(R.id.activity_event_example)
同时将您在onStart方法中拥有的代码移动到onCreate(它被调用一次,而每次屏幕被调到前台时调用onStart),但调用setContentView后
答案 1 :(得分:0)
通常,创建GUI内容的代码应该进入onCreate()
,您需要在setContentView()
之前调用findViewById()
你的课应该是这样的:
public class EventExampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_example);
Button button =(Button)findViewById(R.id.myButton);
button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText("Button clicked");
}
}
);
}
}
更新: 实际上它不应该读R.id.但R.layout。 我编辑了代码,现在它应该是正确的。
答案 2 :(得分:0)
试试这个 -
public class EventExampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_example);
Button button =(Button)findViewById(R.id.myButton);
button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText("Button clicked");
}
}
);
}
}
在您的活动中,您必须覆盖onCreate方法并致电:setContentView(R.layout.activity_event_example)
。其他答案部分正确,他们都告诉你写 setContentView(R.id.activity_event_example); ,而你应该写 setContentView(R.layout.activity_event_example); 。它应该是 R.layout.your_xml ,因为xmls是在布局类中定义的,而不是在id类中。
如果您在R.java文件中查找,那么您将找到类似
的内容public static final class layout {
public static final int your_xml_name=0x7f030000;
}
所以将id
更改为layout
。