我的布局包含很少的标签和文字视图,我想根据操作进行更新。为了测试,我已经设置了java MainActivity
来加载我想要在创建时使用的文本视图。
一旦我点击一个名为add的按钮,就会执行代码并打印出按下添加按钮的内容。
我还希望文本区域说添加进行测试。稍后在项目中,我将更改此处显示的内容。
然而,一旦我点击按钮,程序就会停止。我知道按钮有效,因为打印出现在日志中。
我看了看,我能找到的只有textfield.settext();
但是当使用它时会发生错误并且程序停止。
以下是我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>
以下是MainActivity
中的java代码:
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;
}
}
}
我知道问题出现在tfsubtotal.setText("add");
行,因为我已经测试过没有它,一切正常。
我还尝试使用编辑文本框而不是textview,但我得到了相同的错误。
任何人都可以提出我可能做错的建议吗?
谢谢。
以下是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
答案 0 :(得分:0)
试试这个:
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");
}
});
}