我承认对Android开发和Java一般来说都很新颖,在我看到很多其他人也在搜索的东西中遇到了一些问题,但其他解决方案似乎并不像为我做这件事。我有这个确切的代码在另一个文件中工作,将它复制到另一个程序(从NavigationDrawer切换到NavigationView),现在Fragment在尝试访问TextView时给了我一个NullPointerException。从片段中访问TextView中的布局
就在我可以告诉我在定义视图之前试图拨打findViewById
。我绝对肯定,问题出在方法show_life1_total()
与线
TextView life1_total = (TextView) view.findViewById(R.id.life1_total);
,但我无法找出什么是错的线,因为它在不同的文件中完美地工作。非常感激任何的帮助。或者,如果还有其他方式,我应该在处理片段中的按钮和视图,请赐教。他们在活动中非常容易,但NavigationView迫使我使用片段,并且为我打算在片段中使用的每个按钮设置onClickListeners
,看起来它将是一个巨大的应该是不必要的工作。也许我错过了一些明显的东西。
这里是我的片段
package com.android4dev.navigationview;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Admin on 04-06-2015.
*/
public class GameFragment extends Fragment implements View.OnClickListener{
public static Integer nlife1Total=20;
public static Integer nlife2Total=20;
View view;
Button life1_minus;
Button life1_plus;
Button life2_minus;
Button life2_plus;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_life_counter,container,false);
life1_minus = (Button) view.findViewById(R.id.life1_minus);
life1_minus.setOnClickListener(this);
life1_plus = (Button) view.findViewById(R.id.life1_plus);
life1_plus.setOnClickListener(this);
life2_minus = (Button) view.findViewById(R.id.life2_minus);
life2_minus.setOnClickListener(this);
life2_plus = (Button) view.findViewById(R.id.life2_plus);
life2_plus.setOnClickListener(this);
show_life1_total();
show_life2_total();
return view;
}
public void startNewGame()
{
nlife1Total=20;
nlife2Total=20;
//show_life1_total();
//show_life2_total();
}
public void show_life1_total() {
TextView life1_total = (TextView) view.findViewById(R.id.life1_total);
String display_life1_total = String.valueOf(nlife1Total);
life1_total.setText(display_life1_total);
}
public void show_life2_total() {
TextView life2_total = (TextView) view.findViewById(R.id.life2_total);
String display_life2_total = String.valueOf(nlife2Total);
life2_total.setText(display_life2_total);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.life1_minus:
nlife1Total--;
show_life1_total();
break;
case R.id.life1_plus:
nlife1Total++;
show_life1_total();
break;
case R.id.life2_minus:
nlife2Total--;
show_life2_total();
break;
case R.id.life2_plus:
nlife2Total++;
show_life2_total();
break;
}
}
}
从XML文件中的相关观点(这是fragment_life_counter.xml)
<TextView
android:id="@+id/life1_total"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="140sp"
android:text="@string/life1_total"
android:fontFamily="sans-serif-thin"
android:textColor="@color/life_color"
android:background="@drawable/backdrop_ubr_grixis" />
随着
08-27 22:17:46.103 18943-18943/com.android4dev.navigationview E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.android4dev.navigationview, PID: 18943
java.lang.NullPointerException
at com.android4dev.navigationview.GameFragment.show_life1_total(GameFragment.java:69)
at com.android4dev.navigationview.GameFragment.onCreateView(GameFragment.java:54)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
完美,非常感谢!我知道我已经在全球宣布它,所以show_life1_total()可以找到它,我错过了在该行开头的“视图”在本地再次声明它,而不是使用全局。感谢帮助! –
现在接受。这让我等了5分钟才让我接受。 –