简体   繁体   English

我无法使按钮正常工作

[英]I can't make my buttons work

I just wanted to start making aps on Android, I know C++ a bit, but I'm fighting for few days with the simpliest thing - I can't make my buttons work. 我只是想开始在Android上制作Ap,我对C ++有点了解,但是我为了最简单的事情而奋斗了几天-我无法使按钮正常工作。

I try every tutorials methods and I can't make the listener to button - everytime I do it my program just crushes. 我尝试了所有教程方法,但我无法使按钮成为听众-每次这样做,我的程序都会崩溃。

The code is simple (I don't change anything below my methods) and I also started brand new Android Project and just did those button and TextView edits as in all the tutorials: 代码很简单(我的方法下面没有做任何更改),我也启动了全新的Android Project,并像所有教程一样对按钮和TextView进行了编辑:

package com.example.buttonwork;

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 {
Button but1;
TextView tv1;
@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();
    }
    tv1 = (TextView) findViewById(R.id.tv1);
    but1 = (Button) findViewById(R.id.b1);
    but1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv1.setText("Button pressed");
        }
    });
}


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

} }

The LOGCAT:
04-26 15:51:39.435: W/dalvikvm(7185): threadid=1: thread exiting with uncaught exception (group=0x418762a0)
04-26 15:51:39.450: E/AndroidRuntime(7185): FATAL EXCEPTION: main
04-26 15:51:39.450: E/AndroidRuntime(7185): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttonwork/com.example.buttonwork.MainActivity}: java.lang.NullPointerException
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.os.Looper.loop(Looper.java:137)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.ActivityThread.main(ActivityThread.java:4921)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at java.lang.reflect.Method.invokeNative(Native Method)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at java.lang.reflect.Method.invoke(Method.java:511)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at dalvik.system.NativeStart.main(Native Method)
04-26 15:51:39.450: E/AndroidRuntime(7185): Caused by: java.lang.NullPointerException
04-26 15:51:39.450: E/AndroidRuntime(7185):     at com.example.buttonwork.MainActivity.onCreate(MainActivity.java:29)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.Activity.performCreate(Activity.java:5206)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-26 15:51:39.450: E/AndroidRuntime(7185):     at        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
    04-26 15:51:39.450: E/AndroidRuntime(7185):     ... 11 more

And the program everytime crashes. 程序每次崩溃。 I have everything imported, the IDs and names are also good. 我已经导入了所有内容,ID和名称也都不错。 Please help me because now I am desperate. 请帮助我,因为现在我绝望了。 I just wanted to run through those simple things and start coding something that really works and have and output, but I am stuck in such a simple step... 我只是想遍历那些简单的事情,并开始编写一些确实有效并可以输出的东西,但是我陷入了这样一个简单的步骤……

Here you go .. this is where you were making that mistake . 在这里,你去了 ..这就是你犯那个错误的地方 You were trying to get the reference in you activity instead of the fragment you have defined. 您正在尝试在活动中获取参考,而不是已定义的片段。

Remove the buttons from your onCreate() and in you place holder fragment, add those components. onCreate()中 删除按钮,然后在占位符片段中添加这些组件。 That's the reason you were getting a null pointer. 这就是您获得空指针的原因。

NOTE: use getActivity() to reference your components in a fragment. 注意:使用getActivity()在片段中引用您的组件。

In you fragment you gotta make that change like this: 在片段中,您必须像这样进行更改:

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 onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        Button but1 = (Button)getActivity(). findViewById(R.id.b1);
        TextView tv1 = (TextView)getActivity(). findViewById(R.id.tv1);

        but1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                tv1.setText("Button pressed");

            }
        });
    }
}

Hope this helps.. :) 希望这可以帮助.. :)

Use this and remove tb1 and bt1 initialization code from onCreate(Bundle savedInstanceState) method 使用此方法并从onCreate(Bundle savedInstanceState)方法中删除tb1bt1初始化代码

/**
 * 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);
 tv1 = (TextView) (rootView.findViewById(R.id.tv1));
    but1 = (Button) (rootView.findViewById(R.id.b1));
    but1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv1.setText("Button pressed");
        }
    });
        return rootView;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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