新手,刚开始学习Android,遇到难题(生命周期函数),求助啊,望大神们支招。。。

AndroidManifest.xml中:<?xml version="1.0" encoding="utf-8"?>
package="com.yang.lifecyle"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.yang.lifecyle.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name="com.yang.lifecyle.OtherActivity"
        android:label="name">
        </activity>
</application>

MainActivity.java

package com.yang.lifecyle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

private Button button;
private ButtonListener buttonListener;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonListener=new ButtonListener();
    button=(Button)findViewById(R.id.button);
    button.setOnClickListener(buttonListener);


}

class ButtonListener implements OnClickListener{
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    Intent intent=new Intent();
    intent.setClass(MainActivity.this, OtherActivity.class);
    intent.putExtra("com.yang.lifecyle.Age", 20);
    startActivity(intent);

    }

}

fragment_main
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yang.lifecyle.MainActivity$PlaceholderFragment" >

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="start" />

**
OtherActivity.java**

package com.yang.lifecyle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

public class OtherActivity extends ActionBarActivity{
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
Intent intent=getIntent();
int age=intent.getIntExtra("com.yang.lifecyle.Age",10);
textView=(TextView)findViewById(R.id.textView);
textView.setText("age "+age);

}   

}

other_activity.xml

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView 
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="第二个Activity"
    />

LogCat

08-07 13:32:52.033: W/dalvikvm(391): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-07 13:32:52.133: E/AndroidRuntime(391): FATAL EXCEPTION: main
08-07 13:32:52.133: E/AndroidRuntime(391): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yang.lifecyle/com.yang.lifecyle.MainActivity}: java.lang.NullPointerException
08-07 13:32:52.133: E/AndroidRuntime(391): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
08-07 13:32:52.133: E/AndroidRuntime(391): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
08-07 13:32:52.133: E/AndroidRuntime(391): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-07 13:32:52.133: E/AndroidRuntime(391): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
08-07 13:32:52.133: E/AndroidRuntime(391): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 13:32:52.133: E/AndroidRuntime(391): at android.os.Looper.loop(Looper.java:123)
08-07 13:32:52.133: E/AndroidRuntime(391): at android.app.ActivityThread.main(ActivityThread.java:3647)
08-07 13:32:52.133: E/AndroidRuntime(391): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 13:32:52.133: E/AndroidRuntime(391): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 13:32:52.133: E/AndroidRuntime(391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-07 13:32:52.133: E/AndroidRuntime(391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-07 13:32:52.133: E/AndroidRuntime(391): at dalvik.system.NativeStart.main(Native Method)
08-07 13:32:52.133: E/AndroidRuntime(391): Caused by: java.lang.NullPointerException
08-07 13:32:52.133: E/AndroidRuntime(391): at com.yang.lifecyle.MainActivity.onCreate(MainActivity.java:20)
08-07 13:32:52.133: E/AndroidRuntime(391): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-07 13:32:52.133: E/AndroidRuntime(391): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
08-07 13:32:52.133: E/AndroidRuntime(391): ... 11 more
08-07 13:32:52.153: W/ActivityManager(61): Force finishing activity com.yang.lifecyle/.MainActivity
08-07 13:32:52.659: W/ActivityManager(61): Activity pause timeout for HistoryRecord{406fee18 com.yang.lifecyle/.MainActivity}
08-07 13:33:03.612: W/ActivityManager(61): Activity destroy timeout for HistoryRecord{406fee18 com.yang.lifecyle/.MainActivity}
08-07 13:33:05.007: W/ActivityManager(61): Activity idle timeout for HistoryRecord{408b0e00 com.yang.lifecyle/.MainActivity}

查看全部
out_going
out_going
2015/08/07 13:51
  • exception
  • android
  • 点赞
  • 收藏
  • 回答
    私信

3个回复