Android App在启动时崩溃 - setOnClickListener

时间:2014-04-09 10:17:39

标签: android

我是Android开发人员的新手但尝试构建一个简单的计算器,问题是我无法加载按钮。我已将其简化为单个按钮更改TextView但在启动时崩溃。

如果我注释掉button1.setOnClickListener(this);它将在手机上运行(Nexus 4),但显然这意味着该按钮无效。

我在课堂上实现了onClickListener,因为我认为每个按钮都有一个新的监听器更简洁,更容易 - 最后会有很多按钮。

任何人都可以看到我做错了吗? 感谢

package com.example.newcalc;

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.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener {

Button button1;
TextView textview1;

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

    button1 = (Button) findViewById (R.id.button_1);
    textview1 = (TextView) findViewById (R.id.display);

//Commenting out the setOnClickListener stops the start error but obviously means the button does not work.
    button1.setOnClickListener(this);

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

@Override
public void onClick(View v) {
    textview1.setText("1");

}

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。虽然我现在只引用一个按钮和一个文本视图。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.example.newcalc.MainActivity$PlaceholderFragment" >


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/workings"
    android:textSize="20sp" />

<TextView
    android:id="@+id/display"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:paddingBottom="20dp"
    android:text="@string/display"
    android:textSize="60sp" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >



    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_ClearAll" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_ClearCurrent"  />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_MinusModifier"  />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Memory" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_7"   />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_8" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_9" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Divide" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_4" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_5" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_6" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Multiply" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Minus" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_0" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Point" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Equals" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Plus" />
</LinearLayout>

</LinearLayout>

这是logcat(我希望我做对了)。我假设它是NullPointerException?

04-09 12:12:44.089: E/AndroidRuntime(27649): FATAL EXCEPTION: main
04-09 12:12:44.089: E/AndroidRuntime(27649): Process: com.example.newcalc, PID: 27649
04-09 12:12:44.089: E/AndroidRuntime(27649): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newcalc/com.example.newcalc.MainActivity}: java.lang.NullPointerException
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.os.Looper.loop(Looper.java:136)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at java.lang.reflect.Method.invoke(Method.java:515)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at dalvik.system.NativeStart.main(Native Method)
04-09 12:12:44.089: E/AndroidRuntime(27649): Caused by: java.lang.NullPointerException
04-09 12:12:44.089: E/AndroidRuntime(27649):    at com.example.newcalc.MainActivity.onCreate(MainActivity.java:27)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.app.Activity.performCreate(Activity.java:5231)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-09 12:12:44.089: E/AndroidRuntime(27649):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-09 12:12:44.089: E/AndroidRuntime(27649):    ... 11 more
04-09 12:12:53.909: E/WindowManager(592): Starting window AppWindowToken{436a7be8 token=Token{43497d70 ActivityRecord{43406b18 u0 com.example.newcalc/.MainActivity t100}}} timed out

再次感谢

3 个答案:

答案 0 :(得分:2)

我想问题是您尝试从activity_main.xml访问,但它们位于fragment_main.xml

setContentView(R.layout.activity_main);
button1 = (Button) findViewById (R.id.button_1);
textview1 = (TextView) findViewById (R.id.display);

答案 1 :(得分:1)

尝试setOnClickListener这样的

button1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    textview1.setText("1");
                }
            });

试试这个..

修改

将fragment_main.xml中的整个代码复制到activity_main.xml ..我认为这应该可以正常工作

答案 2 :(得分:0)

fragment_main.xml中的整个代码复制到Activity_main.xml。我认为这应该有用