Android的文本视图试图设置文本时给了一个错误 [英] Android Text View giving an error when trying to set text

查看:200
本文介绍了Android的文本视图试图设置文本时给了一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个标签,这是我想基于一个操作以更新textviews布局。为了测试我已成立了Java MainActivity 加载我要上创建的。使用textviews

在我点击了一个名为Add按钮,则执行code和打印的附加按钮被按下。

我也想文本区域说添加进行测试。后来在这个项目我会改变什么在这里显示。

然而,当我点击程序停止的按钮。我知道按钮的作品,因为打印在日志中显示出来。

我看了又看,所有我能找到的是 textfield.settext(); 但是使用这个错误发生时,程序停止

下面是我的XML的的TextView

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
的xmlns:android1 =htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#000066
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=$ com.example.smartshop.MainActivity PlaceholderFragment><的LinearLayout
    android1:ID =@ + ID / linearLayout1
    android1:layout_width =WRAP_CONTENT
    android1:layout_height =WRAP_CONTENT
    android1:方向=垂直>    < ListView控件
        android1:ID =@ + ID / cartlist
        android1:layout_width =289dp
        android1:layout_height =236dp
        android1:文字颜色=@色/ textc>
    < /&的ListView GT;    <的LinearLayout
        android1:layout_width =match_parent
        android1:layout_height =WRAP_CONTENT>        <按钮
            android1:ID =@ + ID / btnadd
            android1:layout_width =0pt
            android1:layout_height =WRAP_CONTENT
            android1:layout_weight =1
            android1:背景=@彩色/ buttonc
            android1:的onClick =clkaddbutton
            android1:文本=添加项目/>
        !< - android1:的onClick =clkaddbutton - >        < android.support.v7.widget.Space
            android1:ID =@ + ID / space1
            android1:layout_width =2DP
            android1:layout_height =WRAP_CONTENT/>        <按钮
            android1:ID =@ + ID / btnremove
            android1:layout_width =0pt
            android1:layout_height =WRAP_CONTENT
            android1:layout_weight =1
            android1:背景=@彩色/ buttonc
            android1:的onClick =clkremovebutton
            android1:文本=删除项目/>
        !< - android1:的onClick =clkremovebutton - >    < / LinearLayout中>
<查看
机器人:layout_width =1DP
机器人:layout_height =@扪/ verticalspacesize>
< /视图>
    <的LinearLayout
        android1:layout_width =match_parent
        android1:layout_height =WRAP_CONTENT>        <的TextView
            android1:ID =@ + ID / lblsubtotal
            android1:layout_width =WRAP_CONTENT
            android1:layout_height =WRAP_CONTENT
            android1:layout_weight =3.40
            android1:文本=小计
            android1:TEXTSIZE =@扪/ TEXTSIZE
            android1:文字颜色=@色/ textc/>        <的TextView
            android1:ID =@ + ID / tvsubtotal
            android1:layout_width =WRAP_CONTENT
            android1:layout_height =WRAP_CONTENT
            android1:layout_weight =3.40
            android1:TEXTSIZE =@扪/ TEXTSIZE
            android1:文本=TextView的/>
    < / LinearLayout中>
<查看
机器人:layout_width =1DP
机器人:layout_height =@扪/ verticalspacesize>
< /视图>
    <的LinearLayout
        android1:layout_width =match_parent
        android1:layout_height =WRAP_CONTENT>
        <的TextView
            android1:ID =@ + ID / lbltax
            android1:layout_width =WRAP_CONTENT
            android1:layout_height =WRAP_CONTENT
            android1:layout_weight =1
            android1:文本=税
            android1:TEXTSIZE =@扪/ TEXTSIZE
            android1:文字颜色=@色/ textc/>        <的TextView
            android1:ID =@ + ID / tvtax
            android1:layout_width =WRAP_CONTENT
            android1:layout_height =WRAP_CONTENT
            android1:layout_weight =1
            android1:TEXTSIZE =@扪/ TEXTSIZE
            android1:文本=TextView的/>
    < / LinearLayout中><查看
机器人:layout_width =1DP
机器人:layout_height =@扪/ verticalspacesize>
< /视图>    <的LinearLayout
        android1:layout_width =match_parent
        android1:layout_height =WRAP_CONTENT>        <的TextView
            android1:ID =@ + ID / lbltotal
            android1:layout_width =WRAP_CONTENT
            android1:layout_height =WRAP_CONTENT
            android1:layout_weight =1
            android1:文本=总计
            android1:TEXTSIZE =@扪/ TEXTSIZE
            android1:文字颜色=@色/ textc/>        <的TextView
            android1:ID =@ + ID / tvtotal
            android1:layout_width =WRAP_CONTENT
            android1:layout_height =WRAP_CONTENT
            android1:layout_weight =1
            android1:TEXTSIZE =@扪/ TEXTSIZE
            android1:文本=TextView的/>
    < / LinearLayout中>
 < / LinearLayout中>< / RelativeLayout的>

下面是在java code中的 MainActivity

 包com.example.smartshop;进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.os.Build;进口的java.lang。*;公共类MainActivity扩展ActionBarActivity {
    私人按钮btnadd;
    私人按钮btnremove;
    私人TextView的tfsubtotal;
    私人TextView的tftax;
    私人TextView的tftotal;
         公共无效clkaddbutton(查看视图){
             的System.out.println(添加点击);
           tfsubtotal.setText(添加);
         }
        公共无效clkremovebutton(查看视图){
         tfsubtotal.setText(REM);
         }
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        btnremove =(按钮)findViewById(R.id.btnremove);
        btnadd =(按钮)findViewById(R.id.btnadd);
        tfsubtotal =(的TextView)findViewById(R.id.tvsubtotal);
        如果(tfsubtotal!= NULL){
        的System.out.println(小计EditText上创建);
        tfsubtotal.setText(测试);
        }
        其他{
            的System.out.println(TextView的为空);
        }
        //tfsubtotal.setText(\"Google是你的朋友)。
        tftax =(的TextView)findViewById(R.id.tvtax);
        tftotal =(的TextView)findViewById(R.id.tvtotal);        如果(savedInstanceState == NULL){
            getSupportFragmentManager()调用BeginTransaction()
            。新增(R.id.container,新PlaceholderFragment())
            。承诺();
        }
    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }    / **
     *包含一个简单视图中的占位符片段。
     * /
    公共静态类PlaceholderFragment扩展片段{        公共PlaceholderFragment(){
        }        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);
            返回rootView;
        }
    }}

我知道这个问题是关于 tfsubtotal.setText(加); 行,因为我已经没有它测试,一切正常。

我也试过用编辑的文本框,而不是textviews,但我得到了同样的错误。

任何人都可以提出什么,我可能是做错了?

感谢您。

下面是Android日志:

  05-22 22:37:21.251:我/的System.out(1693):小计的EditText创建
05-22 22:37:21.251:D / AndroidRuntime(1693):关闭VM
05-22 22:37:21.261:W / dalvikvm(1693):主题ID = 1:螺纹未捕获的异常(组= 0xb2accba8)退出
05-22 22:37:21.271:E / AndroidRuntime(1693):致命异常:主要
05-22 22:37:21.271:E / AndroidRuntime(1693):工艺:com.example.smartshop,PID:1693
05-22 22:37:21.271:E / AndroidRuntime(1693):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.smartshop / com.example.smartshop.MainActivity}:显示java.lang.NullPointerException
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.app.ActivityThread.access $ 800(ActivityThread.java:135)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.os.Handler.dispatchMessage(Handler.java:102)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.os.Looper.loop(Looper.java:136)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.app.ActivityThread.main(ActivityThread.java:5017)
05-22 22:37:21.271:E / AndroidRuntime(1693):在java.lang.reflect.Method.invokeNative(本机方法)
05-22 22:37:21.271:E / AndroidRuntime(1693):在java.lang.reflect.Method.invoke(Method.java:515)
05-22 22:37:21.271:E / AndroidRuntime(1693):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
05-22 22:37:21.271:E / AndroidRuntime(1693):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-22 22:37:21.271:E / AndroidRuntime(1693):在dalvik.system.NativeStart.main(本机方法)
05-22 22:37:21.271:E / AndroidRuntime(1693):因:显示java.lang.NullPointerException
05-22 22:37:21.271:E / AndroidRuntime(1693):在com.example.smartshop.MainActivity.onCreate(MainActivity.java:42)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.app.Activity.performCreate(Activity.java:5231)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-22 22:37:21.271:E / AndroidRuntime(1693):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-22 22:37:21.271:E / AndroidRuntime(1693):11 ...更多
05-22 22:37:23.451:I /流程(1693):发送信号。 PID:1693 SIG:9


解决方案

试试这个:

 公共类MainActivity扩展ActionBarActivity {
私人按钮btnadd;
私人按钮btnremove;
私人TextView的tfsubtotal;
私人TextView的tftax;
私人TextView的tftotal;保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    btnadd =(按钮)findViewById(R.id.btnadd);
    的System.out.println(小计EditText上创建);
    //tfsubtotal.setText(\"Google是你的朋友)。
    tftax =(的TextView)findViewById(R.id.tvtax);
    TextView的tfsubtotal =(的TextView)findViewById(R.id.tvsubtotal);
    tftotal =(的TextView)findViewById(R.id.tvtotal);
    如果(savedInstanceState == NULL){
        getSupportFragmentManager()调用BeginTransaction()
        。新增(R.id.container,新PlaceholderFragment())
        。承诺();
    }
}
公共无效clkaddbutton(查看视图){
        // tfsubtotal。
       的System.out.println(添加点击);
       tfsubtotal.setText(添加);
     }
    公共无效clkremovebutton(查看视图){
     tfsubtotal.setText(REM);
     }

为什么不只是做简单的是这样的:

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    btnadd =(按钮)findViewById(R.id.btnadd);
    按钮btnRemove =(按钮)findViewById(R.id.btnremove);
    tfsubtotal =(的TextView)findViewById(R.id.tvsubtotal);
    的System.out.println(小计EditText上创建);
    //tfsubtotal.setText(\"Google是你的朋友)。
    tftax =(的TextView)findViewById(R.id.tvtax);
    tftotal =(的TextView)findViewById(R.id.tvtotal);    如果(savedInstanceState == NULL){
        getSupportFragmentManager()调用BeginTransaction()
        。新增(R.id.container,新PlaceholderFragment())
        。承诺();
    }    btnadd.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(视图v){    的System.out.println(添加点击);
    tfsubtotal.setText(添加);    }    });    btnRemove.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(视图v){    的System.out.println(添加点击);
    tfsubtotal.setText(REM);    }    });}

I have a layout with few labels and textviews which I want to update based on a action. For testing I have set up the java MainActivity to load the textviews I want to use on created.

Once I click a button called add, the code is executed and prints that the add button was pushed.

I also want the text area to say add for testing. Later on in the project I will change what is displayed here.

However, once I click the button the program stops. I know that the button works because the print shows up in the log.

I have looked and looked and all I can find is the textfield.settext(); But when using this the error occurs and the program stops.

Below is my XML for the TextView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000066"
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.smartshop.MainActivity$PlaceholderFragment" >

<LinearLayout
    android1:id="@+id/linearLayout1"
    android1:layout_width="wrap_content"
    android1:layout_height="wrap_content"
    android1:orientation="vertical" >

    <ListView
        android1:id="@+id/cartlist"
        android1:layout_width="289dp"
        android1:layout_height="236dp"
        android1:textColor="@color/textc" >
    </ListView>

    <LinearLayout
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content" >

        <Button
            android1:id="@+id/btnadd"
            android1:layout_width="0pt"
            android1:layout_height="wrap_content"
            android1:layout_weight="1"
            android1:background="@color/buttonc"
            android1:onClick="clkaddbutton"
            android1:text="Add Item" />
        <!-- android1:onClick="clkaddbutton" -->

        <android.support.v7.widget.Space
            android1:id="@+id/space1"
            android1:layout_width="2dp"
            android1:layout_height="wrap_content" />

        <Button
            android1:id="@+id/btnremove"
            android1:layout_width="0pt"
            android1:layout_height="wrap_content"
            android1:layout_weight="1"
            android1:background="@color/buttonc"
            android1:onClick="clkremovebutton"
            android1:text="Remove Item" />
        <!-- android1:onClick = "clkremovebutton" -->

    </LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="@dimen/verticalspacesize">
</View>
    <LinearLayout
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content" >

        <TextView
            android1:id="@+id/lblsubtotal"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_weight="3.40"
            android1:text="Sub Total"
            android1:textSize="@dimen/textsize"
            android1:textColor="@color/textc" />

        <TextView
            android1:id="@+id/tvsubtotal"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_weight="3.40"
            android1:textSize="@dimen/textsize"
            android1:text="TextView" />
    </LinearLayout>


<View
android:layout_width="1dp"
android:layout_height="@dimen/verticalspacesize">
</View>
    <LinearLayout
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content" >
        <TextView
            android1:id="@+id/lbltax"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_weight="1"
            android1:text="Tax"
            android1:textSize="@dimen/textsize"
            android1:textColor="@color/textc" />

        <TextView
            android1:id="@+id/tvtax"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_weight="1"
            android1:textSize="@dimen/textsize"
            android1:text="TextView" />
    </LinearLayout>

<View
android:layout_width="1dp"
android:layout_height="@dimen/verticalspacesize">
</View>

    <LinearLayout
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content" >

        <TextView
            android1:id="@+id/lbltotal"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_weight="1"
            android1:text="Total"
            android1:textSize="@dimen/textsize"
            android1:textColor="@color/textc" />

        <TextView
            android1:id="@+id/tvtotal"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_weight="1"
            android1:textSize="@dimen/textsize"
            android1:text="TextView" />
    </LinearLayout>
 </LinearLayout>

</RelativeLayout>

Here is the java code in the MainActivity:

package com.example.smartshop;

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

import java.lang.*;

public class MainActivity extends ActionBarActivity {
    private Button btnadd; 
    private Button btnremove;
    private TextView tfsubtotal ;
    private TextView tftax; 
    private TextView tftotal; 
         public void clkaddbutton(View view) {
             System.out.println("Add clicked");
           tfsubtotal.setText("add");
         }
        public void clkremovebutton(View view) {
         tfsubtotal.setText("Rem");
         }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnremove = (Button) findViewById(R.id.btnremove);
        btnadd = (Button) findViewById(R.id.btnadd);
        tfsubtotal = (TextView) findViewById(R.id.tvsubtotal);
        if(tfsubtotal != null){
        System.out.println("The subtotal edittext was created");
        tfsubtotal.setText("test" );
        }
        else{
            System.out.println("The textview is null");
        }
        //tfsubtotal.setText("Google is your friend." );
        tftax = (TextView) findViewById(R.id.tvtax);
        tftotal = (TextView) findViewById(R.id.tvtotal);

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

}

I know that the problem is on the tfsubtotal.setText("add"); line because I have tested without it and everything works.

I have also tried to use edit text boxes instead of textviews but I get the same error.

Can anyone suggest what I might be doing wrong?

Thank you.

Below is the android log:

05-22 22:37:21.251: I/System.out(1693): The subtotal edittext was created
05-22 22:37:21.251: D/AndroidRuntime(1693): Shutting down VM
05-22 22:37:21.261: W/dalvikvm(1693): threadid=1: thread exiting with uncaught exception (group=0xb2accba8)
05-22 22:37:21.271: E/AndroidRuntime(1693): FATAL EXCEPTION: main
05-22 22:37:21.271: E/AndroidRuntime(1693): Process: com.example.smartshop, PID: 1693
05-22 22:37:21.271: E/AndroidRuntime(1693): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smartshop/com.example.smartshop.MainActivity}: java.lang.NullPointerException
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.os.Looper.loop(Looper.java:136)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at java.lang.reflect.Method.invoke(Method.java:515)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at dalvik.system.NativeStart.main(Native Method)
05-22 22:37:21.271: E/AndroidRuntime(1693): Caused by: java.lang.NullPointerException
05-22 22:37:21.271: E/AndroidRuntime(1693):     at com.example.smartshop.MainActivity.onCreate(MainActivity.java:42)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.app.Activity.performCreate(Activity.java:5231)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-22 22:37:21.271: E/AndroidRuntime(1693):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-22 22:37:21.271: E/AndroidRuntime(1693):     ... 11 more
05-22 22:37:23.451: I/Process(1693): Sending signal. PID: 1693 SIG: 9

解决方案

Try this:

public class MainActivity extends ActionBarActivity {
private Button btnadd; 
private Button btnremove;
private TextView tfsubtotal ;
private TextView tftax; 
private TextView tftotal; 

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnadd = (Button) findViewById(R.id.btnadd);
    System.out.println("The subtotal edittext was created");
    //tfsubtotal.setText("Google is your friend." );
    tftax = (TextView) findViewById(R.id.tvtax);
    TextView tfsubtotal = (TextView) findViewById(R.id.tvsubtotal);
    tftotal = (TextView) findViewById(R.id.tvtotal);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
        .add(R.id.container, new PlaceholderFragment())
        .commit();
    }
}
public void clkaddbutton(View view) {
        // tfsubtotal.
       System.out.println("Add clicked");
       tfsubtotal.setText("add");
     }
    public void clkremovebutton(View view) {
     tfsubtotal.setText("Rem");
     }

OR

Why not just do simply like this :

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

    btnadd = (Button) findViewById(R.id.btnadd);
    Button btnRemove = (Button)findViewById(R.id.btnremove);
    tfsubtotal = (TextView) findViewById(R.id.tvsubtotal);
    System.out.println("The subtotal edittext was created");
    //tfsubtotal.setText("Google is your friend." );
    tftax = (TextView) findViewById(R.id.tvtax);
    tftotal = (TextView) findViewById(R.id.tvtotal);

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

    btnadd.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {

    System.out.println("Add clicked");
    tfsubtotal.setText("add");

    }

    });

    btnRemove.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {

    System.out.println("Add clicked");
    tfsubtotal.setText("Rem");

    }

    });

}

这篇关于Android的文本视图试图设置文本时给了一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆