更新ADT后无法为onclicklistener设置按钮

时间:2014-04-22 05:49:12

标签: java android adt

更新我的ADT后,我真的很沮丧。以前,新的Android项目只包含activity_main.xml文件,我们在其中添加界面元素。现在,每当我创建一个新的android项目时更新ADT,它就会创建一个fragment_main.xmlactivity_main.xml文件。这是fragment_main.xml的内容是什么?此外,MainActivity.class还包含一些额外的代码,例如名为PlaceholderFragment的类和其他一些奇怪的东西。

现在主要的问题是,我无法为onClickListener中的按钮添加fragment_main.xml

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.cameraproject.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="188dp"
        android:text="Button" />

</RelativeLayout>

MainActivity.class:

package com.example.cameraproject;

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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {
    public static Button b;

    @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();
        }

        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(this);
    }

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            Toast.makeText(this, "Its working", Toast.LENGTH_SHORT).show();
            break;
        }
    }

}

错误日志:

04-22 11:16:54.186: E/AndroidRuntime(17336): FATAL EXCEPTION: main
04-22 11:16:54.186: E/AndroidRuntime(17336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cameraproject/com.example.cameraproject.MainActivity}: java.lang.NullPointerException
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.os.Looper.loop(Looper.java:137)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.app.ActivityThread.main(ActivityThread.java:5493)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at java.lang.reflect.Method.invoke(Method.java:525)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at dalvik.system.NativeStart.main(Native Method)
04-22 11:16:54.186: E/AndroidRuntime(17336): Caused by: java.lang.NullPointerException
04-22 11:16:54.186: E/AndroidRuntime(17336):    at com.example.cameraproject.MainActivity.onCreate(MainActivity.java:28)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.app.Activity.performCreate(Activity.java:5372)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-22 11:16:54.186: E/AndroidRuntime(17336):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)

4 个答案:

答案 0 :(得分:3)

试试这个..

您的按钮属于fragment_main.xml所以您必须在PlaceholderFragment

中对其进行初始化
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }
    Button b;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Its working", Toast.LENGTH_SHORT).show();
        }
    });
        return rootView;
    }
}

答案 1 :(得分:1)

您已将布局文件设置为activity_main.xml ..但您已在fragment_main.xml中添加了按钮..这就是引发空指针异常的原因..

替换此

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);

这是最简单的解决方案......

fragment_main.xml中的所有内容复制到activity_main.xml ...

希望有所帮助......

答案 2 :(得分:0)

重新排列此行

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);

答案 3 :(得分:0)

你的按钮属于片段的布局而不是你的activiti的布局。 因此,从您的activitie的onCreate()中删除按钮视图初始化代码并添加

View rootView = inflater.inflate(R.layout.fragment_main, container, false); Button b = (Button) rootView.findViewById(R.id.button1);

在PlaceholderFragment的onCreateView()中。