Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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
Android TextView正在返回null_Android_Nullpointerexception_Textview - Fatal编程技术网

Android TextView正在返回null

Android TextView正在返回null,android,nullpointerexception,textview,Android,Nullpointerexception,Textview,我有一个监听器,只需更改按钮的文本并为用户计时。我还有一个textview,当用户打卡时,它应该更改为新的总工作时间。我仔细检查了xml以确保我获取了正确的R.Id,并且在侦听器中找到了按钮,而不是TextView。代码如下: public class HoursListener implements OnClickListener{ GlobalApp appState; Button startStopHoursButton; TextView hoursTotalTextView; pu

我有一个监听器,只需更改按钮的文本并为用户计时。我还有一个textview,当用户打卡时,它应该更改为新的总工作时间。我仔细检查了xml以确保我获取了正确的R.Id,并且在侦听器中找到了按钮,而不是TextView。代码如下:

public class HoursListener implements OnClickListener{

GlobalApp appState;
Button startStopHoursButton;
TextView hoursTotalTextView;
public boolean clockedIn;

public HoursListener(Context ctx){
    appState = ((GlobalApp)ctx.getApplicationContext());
}

public void onClick(View v) {
    startStopHoursButton = (Button) v.findViewById(R.id.hourstogglebutton);
    hoursTotalTextView = (TextView) v.findViewById(R.id.totalWorktimeTextView);
    if(!clockedIn){
        startStopHoursButton.setText(R.string.clockout);
        appState.getCurrentCompany().getCurrentNewWeeklyTimestamp().CreateFinishTimeStamp();
        clockedIn = true;
    }else{
        startStopHoursButton.setText(R.string.clockin);
        appState.getCurrentCompany().getCurrentNewWeeklyTimestamp().CreateFinishTimeStamp();
        clockedIn = false;
        hoursTotalTextView.setText(appState.getCurrentCompany().getCurrentNewWeeklyTimestamp().totalTimeDoneThisWeekToString());                
    }
}
}

以下是textView的xml:

<TextView
        android:id="@+id/totalWorktimeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"/>
我想把代码放在活动中,但我觉得我可以这样做。我想要一些我可以调用的东西,比如一个监听器,这样我就减少了代码中的冗余。我仔细检查了hoursTotalTextView是否为空。但按钮不是。有什么想法吗

Eclipse屏幕截图(链接到全尺寸版本)显示相关值不为空:
什么是GlobalApp appState;以及为什么要从下面的行向GlobalApp键入上下文。 我认为appstate在这里是空的

appState=((GlobalApp)ctx.getApplicationContext())


如果GlobalApp是一个类,那么为它创建一个对象,然后使用getter和setter方法。

什么是GlobalApp appState;以及为什么要从下面的行向GlobalApp键入上下文。 我认为appstate在这里是空的

appState=((GlobalApp)ctx.getApplicationContext())

如果GlobalApp是一个类,那么为它创建一个对象,然后使用getter和setter方法。

您需要使用

startStopHoursButton = (Button) v.findViewById(R.id.hourstogglebutton);
hoursTotalTextView = (TextView) v.findViewById(R.id.totalWorktimeTextView);
以前的一些地方可能是构造函数,因为setText正在处理空TextView对象。

您需要使用

startStopHoursButton = (Button) v.findViewById(R.id.hourstogglebutton);
hoursTotalTextView = (TextView) v.findViewById(R.id.totalWorktimeTextView);

在前面的一些地方,可能是构造函数,导致setText正在处理空TextView对象。

检查该行中的每个对象

if(appState.getCurrentCompany()==null)
     Log.d("","getCurrentCompany is null");
if(appState.getCurrentCompany().getCurrentNewWeeklyTimestamp()==null)
     Log.d("","getCurrentNewWeeklyTimestamp is null");
if(appState.getCurrentCompany().getCurrentNewWeeklyTimestamp().totalTimeDoneThisWeekToString()==null)
     Log.d("","totalTimeDoneThisWeekToString is null");

检查该行中的每个对象

if(appState.getCurrentCompany()==null)
     Log.d("","getCurrentCompany is null");
if(appState.getCurrentCompany().getCurrentNewWeeklyTimestamp()==null)
     Log.d("","getCurrentNewWeeklyTimestamp is null");
if(appState.getCurrentCompany().getCurrentNewWeeklyTimestamp().totalTimeDoneThisWeekToString()==null)
     Log.d("","totalTimeDoneThisWeekToString is null");

您正在收听其单击的onClickListener可能就是按钮?onClick传递被单击的视图v,即按钮,而文本框不是按钮的子项,因此您无法找到它

如果在活动中声明了HoursListener,只需执行findViewById而不是v.findViewById即可

hoursTotalTextView = (TextView) findViewById(R.id.totalWorktimeTextView);

如果没有,请将textview传递到HoursListener构造函数中,并在其中设置hoursTotalTextView(请记住,这可能会导致内存泄漏)。

您正在侦听的onClickListener可能就是按钮?onClick传递被单击的视图v,即按钮,而文本框不是按钮的子项,因此您无法找到它

如果在活动中声明了HoursListener,只需执行findViewById而不是v.findViewById即可

hoursTotalTextView = (TextView) findViewById(R.id.totalWorktimeTextView);


如果没有,则将textview传递到HoursListener构造函数中,并在其中设置hoursTotalTextView(请记住,这可能会导致内存泄漏).

GlobalApp是我制作的应用程序,因此我可以访问运行应用程序所需的某些静态变量。您能否粘贴该类,因为我与此行appState.getCurrentCompany().getCurrentNewWeeklyTimestamp().CreateFinishTimeStamp()混淆;就像在扩展类应用程序中一样?你需要改进你的代码GlobalApp或TextView可能为空…我会解释它。App state有一个当前公司,该公司有一个周时间表列表。这些WeeklyTimeStamp会保存该周的时间戳,GetCurrentNewWeeklyTimeStamp()会获取该周的时间戳,或者为该周创建一个新的时间戳。CreateFinishTimestamp启动一个新的时间戳或完成一个已经创建的时间戳。Vincent-是,TextView为空。GlobalApp不是null,否则我会在侦听器中到处抛出null指针。textView是唯一表明其为null的东西。GlobalApp是我制作的应用程序,因此我可以访问运行应用程序所需的某些静态变量。你能粘贴该类吗,因为我与以下行混淆:appState.getCurrentCompany().getCurrentNewWeeklyTimestamp().CreateFinishTimeStamp();就像在扩展类应用程序中一样?你需要改进你的代码GlobalApp或TextView可能为空…我会解释它。App state有一个当前公司,该公司有一个周时间表列表。这些WeeklyTimeStamp会保存该周的时间戳,GetCurrentNewWeeklyTimeStamp()会获取该周的时间戳,或者为该周创建一个新的时间戳。CreateFinishTimestamp启动一个新的时间戳或完成一个已经创建的时间戳。Vincent-是,TextView为空。GlobalApp不是null,否则我会在侦听器中到处抛出null指针。textView是唯一表示为null的东西。每当调用onClick方法时,我都会这样做。请尝试在构造函数中保留它,因为您的textView为null,或者可能使用了错误的xml,我猜…每当调用onClick方法时,我都会这样做。尝试在构造函数中保留它,因为您的textView为null,或者可能使用了错误的xml,我猜…所有这些都不是无效的调试显示TextView为空。您还应注意mainview也是空的。能否提供MainActivity如何创建HoursListener?main.xml是唯一的xml吗?通常,TextView应该在活动类中。也请提供MainActivity.java。所有这些都不是空的。调试显示TextView为空。您还应注意mainview也是空的。能否提供MainActivity如何创建HoursListener?main.xml是唯一的xml吗?通常,TextView应该在活动类中。也请提供MainActivity.java。对于静态变量,我有我的应用程序类。对于静态变量,我有我的应用程序类。我所需要的只是将TextView传递给侦听器。非常感谢。我只需要将文本视图传递给听众。非常感谢你。