简体   繁体   中英

my android app crashes as soon as i want to make a new task

hi guys im new to android and i want to make an app.something like a daily planner. the problem is this, my app opens up and i have the new task button, when i hit the button to add a new task the app crashes. i searched the forum for similar problems but couldn't get help.

here's my cat log file:

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

this is the android manifest:

<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>

here is my main.java file:

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;
}

and this is the new_task.java file:

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;

}

Any help?

You can only use findViewById after you set the content view. It is also useful to null check variables before accessing them. Finally, make sure that the layout file R.layout.new_task contains items with the ids timeTxt, dateTxt, btnSave.

I re-wrote some of your work to give you an example of what I mean:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM