按钮Android开发者工具构建:v22.6.2的问题

时间:2014-04-04 04:01:52

标签: android button onclicklistener

我在Android Developer Tools Build的最新版本中遇到了问题:v22.6.2-1085508

在以前的版本只有布局并且还扩展了ActionBarActivity,以前只使用了Activity

,这是非常不同的

Java代码:

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity implements OnClickListener {
    private TextView tv;
    private Button btn1;

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

        tv = (TextView) findViewById(R.id.textView1);
        // add the listener.
        tv.setOnClickListener(this);

        // you can define too a listener to your button1.
        btn1 = (Button) findViewById(R.id.button1);
        btn1.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.textView1:
            tv.setText("hola");
            break;
        case R.id.button1:
            tv.setText("click on button1");
            break;
        default:
            // code..
            break;
        }
    }

}

public class sdfsdsdf {

}

Log Cat:

04-03 07:13:20.305: E/AndroidRuntime(971): FATAL EXCEPTION: main
                   04-03 07:13:20.305: E/AndroidRuntime(971):               java.lang.RuntimeException: Unable to start activity       ComponentInfo{com.example.htapex_ame/com.example.htapex_ame.MainActivity}:    java.lang.NullPointerException
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.os.Handler.dispatchMessage(Handler.java:99)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.os.Looper.loop(Looper.java:137)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.app.ActivityThread.main(ActivityThread.java:5041)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at java.lang.reflect.Method.invokeNative(Native Method)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at java.lang.reflect.Method.invoke(Method.java:511)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at dalvik.system.NativeStart.main(Native Method)
                    04-03 07:13:20.305: E/AndroidRuntime(971): Caused by: java.lang.NullPointerException
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at com.example.htapex_ame.MainActivity.onCreate(MainActivity.java:33)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.app.Activity.performCreate(Activity.java:5104)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                    04-03 07:13:20.305: E/AndroidRuntime(971):  ... 11 more
                    04-03 07:13:20.315: W/ActivityManager(286):   Force finishing activity com.example.htapex_ame/.MainActivity
                    04-03 07:13:20.325: W/WindowManager(286): Failure taking screenshot for (123x164) to layer 21010
                    04-03 07:13:20.525: I/Choreographer(286): Skipped 119 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:20.535: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
                    04-03 07:13:20.715: I/Choreographer(286): Skipped 37 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:20.745: I/Choreographer(286): Skipped 37 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:20.845: W/ActivityManager(286): Activity pause timeout for ActivityRecord{40e18340 u0 com.example.htapex_ame/.MainActivity}
                    04-03 07:13:20.885: D/dalvikvm(405): JIT unchain all for threadid=1
                    04-03 07:13:20.925: I/Choreographer(286): Skipped 111 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:20.965: I/Choreographer(286): Skipped 35 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:21.195: D/dalvikvm(405): GC_FOR_ALLOC freed 1402K, 29% free 4964K/6912K, paused 933ms, total 949ms
                    04-03 07:13:21.555: I/Choreographer(405): Skipped 362 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:21.675: I/Choreographer(286): Skipped 32 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:21.775: I/Choreographer(286): Skipped 41 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:21.835: I/Choreographer(286): Skipped 33 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:21.935: I/Choreographer(286): Skipped 39 frames!  The application may be doing too much work on its main thread.
                    04-03 07:13:21.975: D/dalvikvm(405): GC_FOR_ALLOC freed 665K, 30% free 4877K/6912K, paused 333ms, total 335ms
                    04-03 07:13:22.185: I/Process(971): Sending signal. PID: 971 SIG: 9

xml file fragment_main:

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="19dp"
    android:layout_marginTop="36dp"
    android:text="Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="20dp"
    android:text="TextView" />

</RelativeLayout>

xml文件activity_main:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.button_a.MainActivity"
    tools:ignore="MergeRootFrame" >
</FrameLayout>`

1 个答案:

答案 0 :(得分:0)

首先,开发人员工具的新更新没有任何问题。

如果我没有错,你已经发布了片段的xml而不是活动。 如果你想看看你的活动xml,你会发现对应于它的片段的frameLayout。

为此你需要研究片段和所有片段。

此外,您需要将代码放在片段类的onCreateView中。

   tv = (TextView) rootView.findViewById(R.id.text View1);
    // add the listener.
    tv.setOnClickListener(this);

    // you can define too a listener to your button1.
    btn1 = (Button)rootView.findViewById(R.id.button1);
    btn1.setOnClickListener(this);

将TextView和Button设为静态。