0
我正在做一個自定義的烤麪包,所以我取名爲定製敬酒佈局的xml文件ctoast_view.xml如何創建自定義敬酒
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/checkbox_on_background" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
main.xml中的文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="133dp"
android:text="Button" />
</RelativeLayout>
和CToast主類
package com.example.customtoast;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class CToast extends Activity{
\t private Context mContext;
\t private Button mButton;
\t private LinearLayout cToastView;
\t private TextView toastTextView;
\t @Override
\t protected void onCreate(Bundle savedInstanceState) {
\t \t // TODO Auto-generated method stub
\t \t super.onCreate(savedInstanceState);
\t \t setContentView(R.layout.main);
\t \t mContext = this;
\t \t toastTextView = (TextView) findViewById(R.id.textView1);
\t \t mButton = (Button) findViewById(R.id.button1);
\t \t mButton.setOnClickListener(new OnClickListener() {
\t \t \t @Override
\t \t \t public void onClick(View v) {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t showToast();
\t \t \t }
\t \t });
\t }
\t private void showToast() {
\t \t // TODO Auto-generated method stub
\t \t Toast mToast = new Toast(getApplicationContext());
\t \t mToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
\t \t toastTextView.setText("message!!");
\t \t mToast.setView(getLayoutInflater().inflate(R.layout.ctoast_view,null));
\t \t mToast.setDuration(Toast.LENGTH_LONG);
\t \t mToast.show();
\t }
}
現在我得到了這條線
toastTextView.setText("message!!");
我想是因爲我不能引用到一個NullPointerException在由setContentView設置的另一個佈局文件中查看。我怎樣才能設置我的自定義吐司佈局的文本?