嗨,大家好我刚接触到android,我想制作一个类似日常计划的app.something。 问题是这个,我的应用程序打开,我有新的任务按钮,当我点击按钮添加新任务应用程序崩溃。我在论坛上搜索了类似的问题,但无法得到帮助。
这是我的猫日志文件:
09-27 19:03:46.035: D/dalvikvm(19346): Late-enabling CheckJNI
09-27 19:03:46.440: D/dalvikvm(19346): GC_FOR_ALLOC freed 136K, 4% free 12874K/13319K, paused 31ms
09-27 19:03:46.440: I/dalvikvm-heap(19346): Grow heap (frag case) to 13.127MB for 379472-byte allocation
09-27 19:03:46.465: D/dalvikvm(19346): GC_FOR_ALLOC freed 0K, 4% free 13245K/13703K, paused 20ms
09-27 19:03:46.535: D/AbsListView(19346): Get MotionRecognitionManager
09-27 19:03:49.510: D/CLIPBOARD(19346): Hide Clipboard dialog at Starting input: finished by someone else... !
09-27 19:03:53.625: D/AndroidRuntime(19346): Shutting down VM
09-27 19:03:53.625: W/dalvikvm(19346): threadid=1: thread exiting with uncaught exception (group=0x40c5d1f8)
09-27 19:03:53.635: E/AndroidRuntime(19346): FATAL EXCEPTION: main
09-27 19:03:53.635: E/AndroidRuntime(19346): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dailyplanner/com.example.dailyplanner.new_task}: java.lang.NullPointerException
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.os.Looper.loop(Looper.java:137)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.main(ActivityThread.java:4514)
09-27 19:03:53.635: E/AndroidRuntime(19346): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 19:03:53.635: E/AndroidRuntime(19346): at java.lang.reflect.Method.invoke(Method.java:511)
09-27 19:03:53.635: E/AndroidRuntime(19346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-27 19:03:53.635: E/AndroidRuntime(19346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-27 19:03:53.635: E/AndroidRuntime(19346): at dalvik.system.NativeStart.main(Native Method)
09-27 19:03:53.635: E/AndroidRuntime(19346): Caused by: java.lang.NullPointerException
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.Activity.findViewById(Activity.java:1851)
09-27 19:03:53.635: E/AndroidRuntime(19346): at com.example.dailyplanner.new_task.<init>(new_task.java:19)
09-27 19:03:53.635: E/AndroidRuntime(19346): at java.lang.Class.newInstanceImpl(Native Method)
09-27 19:03:53.635: E/AndroidRuntime(19346): at java.lang.Class.newInstance(Class.java:1319)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
09-27 19:03:53.635: E/AndroidRuntime(19346): ... 11 more
这是android清单:
<manifest package="com.example.dailyplanner"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".new_task"/>
</application>
这是我的main.java文件:
package com.example.dailyplanner;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null );
//db.execSQL("CREATE TABLE IF NOT EXISTS Data1 (Id INT IDENTITY(1,1) NOT NULL, text VARCHAR, time TIME, date DATE)");
Button nTaskBtn = (Button) findViewById(R.id.nTask);
nTaskBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent nTaskScreen = new Intent(getApplicationContext() , new_task.class);
startActivity(nTaskScreen);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
这是new_task.java文件:
package com.example.dailyplanner;
import android.R.string;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.net.UrlQuerySanitizer.ValueSanitizer;
import android.os.Bundle;
import android.renderscript.Sampler.Value;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class new_task extends Activity {
static final int TIME_DIALOG_ID=0;
final TextView timeText = (TextView) findViewById(R.id.timeTxt);
TextView dateText = (TextView) findViewById(R.id.dateTxt);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_task);
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
Button btnSetTime = (Button) findViewById(R.id.btnSetTime);
btnSetTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeText.setText(hourOfDay+":"+minute);
};
};
protected Dialog onCreateDialog(int id){
switch(id){
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, 0, 0, true);
}
return null;
}
任何帮助?
答案 0 :(得分:1)
您只能在设置内容视图后使用findViewById。在访问变量之前检查变量也很有用。最后,确保布局文件R.layout.new_task包含具有ids timeTxt,dateTxt,btnSave的项目。
我重写了你的一些工作,给你一个我的意思的例子:
private TextView timeText;
private TextView dateText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_task);
timeText = (TextView) findViewById(R.id.timeTxt);
dateText = (TextView) findViewById(R.id.dateTxt);
Button btnSave = (Button) findViewById(R.id.btnSave);
if (btnSave != null) {
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
} else {
Log.e("MyActivity", "btnSave was null :(");
}
}
答案 1 :(得分:0)
在调用findViewById()
之后,您必须在onCreate()
中致电setContentView()
。