Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android文本视图在尝试设置文本时出错_Java_Android_Eclipse_Android Fragments - Fatal编程技术网

Java Android文本视图在尝试设置文本时出错

Java Android文本视图在尝试设置文本时出错,java,android,eclipse,android-fragments,Java,Android,Eclipse,Android Fragments,我有一个带有少量标签和文本视图的布局,我想根据一个动作来更新它。为了进行测试,我设置了javaMainActivity,以加载我想要在创建的页面上使用的文本视图 单击一个名为add的按钮后,代码将被执行并打印出按下了add按钮 我还希望文本区域显示addfortesting。稍后在项目中,我将更改此处显示的内容 但是,一旦我点击按钮,程序就会停止。我知道这个按钮是有效的,因为打印显示在日志中 我看了又看,我能找到的只有textfield.settext()但使用此选项时会发生错误,程序停止 下面

我有一个带有少量标签和文本视图的布局,我想根据一个动作来更新它。为了进行测试,我设置了java
MainActivity
,以加载我想要在创建的页面上使用的文本视图

单击一个名为add的按钮后,代码将被执行并打印出按下了add按钮

我还希望文本区域显示addfortesting。稍后在项目中,我将更改此处显示的内容

但是,一旦我点击按钮,程序就会停止。我知道这个按钮是有效的,因为打印显示在日志中

我看了又看,我能找到的只有
textfield.settext()但使用此选项时会发生错误,程序停止

下面是我的
文本视图的XML:

<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>
我知道问题出在
tfsubtotal.setText(“add”)行,因为我在没有它的情况下进行了测试,并且一切正常

我也尝试使用编辑文本框而不是文本视图,但我得到了相同的错误

有人能告诉我可能做错了什么吗

多谢各位

以下是android日志:

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

试试这个:

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

    }

    });

}

为什么不干脆这样做:

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

    }

    });

}

布局xml名称是什么,
activity\u main
fragment\u main
?但是您在哪里调用
clkbuttonadd()
和其他方法并在此处发布堆栈跟踪?xml称为fragment\u main,在xml文件的不同部分调用单击。这是带有堆栈跟踪的日志