Eclipse应用程序虽然适用于android,菜鸟

时间:2014-06-14 16:56:11

标签: java android eclipse

我开始学习如何为Android开发应用程序。这是我的第一个项目(测试),它一直给我同样的错误......“应用程序意外停止了”,我无法弄清楚问题出在哪里。

MainActivity.java

package com.example.layoutxml2;

>import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();

            final TextView text1 = (TextView) findViewById(R.id.textView1);
            Button carregar = (Button) findViewById(R.id.button1);

            carregar.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    text1.setText("Xau Pessoal!");

                }
            });


        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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.example.layoutxml2.MainActivity$PlaceholderFragment"
    android:background="#285178" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/banner"
        android:src="@drawable/androidbanner" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="@string/saudacao"
        android:textSize="26sp"
        android:textColor="#ffffff"
         />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="32dp"
        android:text="@string/carregar"
        android:textColor="#ffffff"
        android:onClick="selfdestruct" />

</RelativeLayout>

logcat的

06-14 16:33:35.084: E/AndroidRuntime(1628): FATAL EXCEPTION: main
06-14 16:33:35.084: E/AndroidRuntime(1628): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layoutxml2/com.example.layoutxml2.MainActivity}: java.lang.NullPointerException
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.os.Looper.loop(Looper.java:130)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at java.lang.reflect.Method.invoke(Method.java:507)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at dalvik.system.NativeStart.main(Native Method)
06-14 16:33:35.084: E/AndroidRuntime(1628): Caused by: java.lang.NullPointerException
06-14 16:33:35.084: E/AndroidRuntime(1628):     at com.example.layoutxml2.MainActivity.onCreate(MainActivity.java:28)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 16:33:35.084: E/AndroidRuntime(1628):     ... 11 more

很抱歉,如果错误是显而易见的,但我真的很新,我希望改进。 感谢您的帮助 :) 问候, 里卡多莫雷拉

1 个答案:

答案 0 :(得分:1)

您在Fragment布局中声明了Button,但在活动中使用它们。将代码移入Fragment类进入onCreateView方法并使用rootview.findViewById()...

package com.example.layoutxml2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);

            final TextView text1 = (TextView) rootView.findViewById(R.id.textView1);
            Button carregar = (Button) rootView.findViewById(R.id.button1);

            carregar.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    text1.setText("Xau Pessoal!");

                }
            });

            return rootView;
        }
    }
}