我正试图通过“新波士顿”关注YouTube上的视频系列。 尝试在吐司上显示变量时出错。 继承人'我做了什么..
首先,我创建了一个textView ..
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/CountView"
android:text="Number Value is 0"
/>
然后,
public class MainActivity extends ActionBarActivity {
int Counter;
TextView countView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Counter = 0;
countView= (TextView)findViewById(R.id.CountView);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void Button1Click(View view) {
Counter+=1;
Toast.makeText(this, "Added 1", Toast.LENGTH_SHORT).show();
countView.setText(String.valueOf(Counter));
}
它说该程序已停止工作。 LogCat日志 - &gt;
FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:3606) at android.view.View.performClick(View.java:4211) at android.view.View$PerformClick.run(View.java:17446) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:153) at android.app.ActivityThread.main(ActivityThread.java:5297) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at android.view.View$1.onClick(View.java:3601) ... 11 more Caused by: java.lang.NullPointerException at com.BreadApplications.program1.MainActivity.Button1Click(MainActivity.java:36) ... 14 more
我刚开始学习Android ..有什么帮助吗?
答案 0 :(得分:1)
更改为
Toast.makeText(this, String.valueOf(Counter), Toast.LENGTH_SHORT).show();
Counter是一个int值(应根据java命名约定命名.c应该小而不是ins)。
如果找不到,则查找带有id的资源ResourceNotFoundException
。有一个需要int,这是一个资源ID。
你想要的是采用CharacterSequence的那个。如果查看文档,第二个参数是CharacterSequence。所以请使用String.valueOf(intvlaue)
public static Toast makeText (Context context, CharSequence text, int duration)
Added in API level 1
Make a standard toast that just contains a text view.
Parameters
context The context to use. Usually your Application or Activity object.
text The text to show. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
编辑:
到编辑后的帖子
视图属于片段。因此,您使用onCreateView
Fragment
rootView.findViewById
中的观看次数
或者,如果将fragment_main.xml
设置为activity,则删除if块和片段相关的代码。
你的教程似乎已经过时了。遵循
NullPointerException accessing views in onCreate()
编辑2:
删除fragment_main.xml中的android:onClick="Button1Click"
按钮
删除
public void Button1Click(View view) {
Counter+=1;
Toast.makeText(this, "Added 1", Toast.LENGTH_SHORT).show();
countView.setText(String.valueOf(Counter));
}
然后在Fragment中
public static class PlaceholderFragment extends Fragment {
TextView countView;
int Countter;
public PlaceholderFragment() {
}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView countView = (TextView)(TrootView.findViewById(R.id.CountView);
Button button = (TextView)(TrootView.findViewById(R.id.buttonid);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Counter+=1;
Toast.makeText(getActivity(), "Added 1", Toast.LENGTH_SHORT).show();
countView.setText(String.valueOf(Counter));
}
});
return rootView; }
}
}
答案 1 :(得分:1)
试试这个:
Toast.makeText(this, ""+Counter, Toast.LENGTH_SHORT).show();//auto type conversion really convinent
答案 2 :(得分:1)
您在toast参数中传递整数值;那就是错误。您必须记住,您只需在Toast参数中传递String值,因此将Integer值设置为String,然后使用Toast显示此值。见这个例子:
public void Button1Click(View v){
Counter+=1;
Toast.makeText(this, "Added 1", Toast.LENGTH_SHORT).show();
//convering integer value to String
Toast.makeText(this, Counter+"", Toast.LENGTH_SHORT).show();
}
答案 3 :(得分:0)
您正在使用makeText (Context context, int resId, int duration)
Toast
。在此功能中,resId
应要使用的字符串资源的资源ID ,但是你传递的只是简单的整数。所以它给出了runtimeerror
所以,试试下面的代码
public void Button1Click(View view) {
Counter+=1;
Toast.makeText(YourActivity.this, "Added 1", Toast.LENGTH_SHORT).show();
//countView.setText(""+Counter);
Toast.makeText(YourActivity.this, String.valueOf(Counter), Toast.LENGTH_SHORT).show();
}
答案 4 :(得分:0)
Toast类的方法makeText被重载。
makeText(Context context, int resId, int duration)
makeText(Context context, CharSequence text, int duration)
您正在使用第一种方法,因为您传递的是int值作为第二个参数。此方法正在搜索具有计数器值的资源ID,并且正在抛出未找到资源的异常。
解决方案是将整数值强制转换为String以调用正确的makeTest方法。
Toast.makeText(this, Integer.toString(Counter), Toast.LENGTH_SHORT).show();