Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我想执行新任务时,我的android应用程序就会崩溃_Java_Android_Crash_Nullpointerexception - Fatal编程技术网

Java 当我想执行新任务时,我的android应用程序就会崩溃

Java 当我想执行新任务时,我的android应用程序就会崩溃,java,android,crash,nullpointerexception,Java,Android,Crash,Nullpointerexception,嗨,伙计们,我是android新手,我想做一个应用程序,比如一个日常计划。 问题是,我的应用程序打开了,我有了新任务按钮,当我点击按钮添加新任务时,应用程序崩溃。我在论坛上搜索了类似的问题,但没有得到帮助 这是我的cat日志文件: 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

嗨,伙计们,我是android新手,我想做一个应用程序,比如一个日常计划。 问题是,我的应用程序打开了,我有了新任务按钮,当我点击按钮添加新任务时,应用程序崩溃。我在论坛上搜索了类似的问题,但没有得到帮助

这是我的cat日志文件:

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
这是新的_task.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;
}
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;

}

有什么帮助吗?

只有在设置内容视图后才能使用findViewById。在访问变量之前对其进行空检查也很有用。最后,确保布局文件R.layout.new_任务包含id为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 :(");
    }
}

在调用
setContentView()
之后,必须在
onCreate()
中调用
findViewById()

这有很多代码要看,你能试着把它缩小到问题所在吗?我知道这是很多代码,但这就是问题所在,我看不到我的代码中有任何错误,所以我可以缩小范围,从日志猫我可以说也可以查看NullPointerExection,但我对这是一个新手,我真的不知道我做错了什么:我不认为你需要导入android.R.string;行一旦删除该行并检查您是否在任何行上遇到任何错误,请尝试为我们提供一些帮助。尝试单击logcat错误,在一个或另一个点上,您将被移动到java文件,该文件可能是错误的主要点。尝试检查是否删除R.string行时未出现错误,尽管正如Samir在下面建议的那样,
在new_任务活动中,将这些引用放入final TextView timeText=(TextView)findViewById(R.id.timeTxt);TextView dateText=(TextView)findViewById(R.id.dateTxt);在setContentView之后(R.layout.new_任务)现在有一个错误时间选择器对话框函数,它表示无法解析timeText Hnx jimmyyy!:就是这样,我只需要在创建和使用findview之前删除时间文本和日期文本。。。在oncreate之后,这就成功了,顺便说一句,stnsave不是空的,它只是没有保存信息的代码,我稍后会添加它…thanx很多:)
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 :(");
    }
}